$this vs $(this) in jQuery

后端 未结 15 652
北恋
北恋 2020-11-29 18:07

I\'ve seen some discussions on SO regarding $(this) vs $this in jQuery, and they make sense to me. (See discussion here for an example.)

Bu

相关标签:
15条回答
  • 2020-11-29 18:39

    You usually use var $this = $(this); to avoid creating a new jQuery object more often than necessary. In case of the code below you only create one object instead of two/four. It is completely unrelated to chainability.

    You could also call it that, $thi$ or anything else (don't use the latter one though, it's ugly :p) as $ is just a simple character in JavaScript, exactly like a-z are.

    0 讨论(0)
  • 2020-11-29 18:40

    Inside $.fn.lockDimensions, this is the jQuery object that had lockDimensions called on it.

    Inside the .each, this now references the DOMElement in the current iteration of the loop. $(this) wraps the DOMElement in a jQuery object, and var $this = $(this); is just saving $(this) in a variable called $this, so the jQuery constructor doesn't need to be called multiple times (if you were to use $(this) instead).

    0 讨论(0)
  • 2020-11-29 18:41
     $this = $(this)
    

    which means that you are assigning the current object to a variable named $this. It is not a keyword.

    It is just a variable name.

    0 讨论(0)
提交回复
热议问题