$this vs $(this) in jQuery

后端 未结 15 651
北恋
北恋 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:16

    $this is just a local copy of this wrapped in jQuery.

    In the long term, keeping a local copy rather than wrapping this each time it is needed is much more efficient.

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

    $this is just an ordinary variable. The $ character is a valid character in variable names, so $this acts the same as any other non-reserved variable name. It's functionally identical to calling a variable JellyBean.

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

    $this is simply a local variable, named that way to remind you of $(this). It saves the work of creating the jQuery version of this, and you can use it a number of times.

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

    $ sign is usually used before variable names in JavaScript to differentiate between general value and jQuery object. So here $this just gets the value of $(this) which returns jQuery object of this. $ is just a part of valid variable name.

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

    It just fills $this variable with $(this), so you do not have to lookup for $(this) element every call. It has better performance

    var $this = $(this);
    
    0 讨论(0)
  • 2020-11-29 18:30

    $this is a variable named $this containing a reference to $(this). A bit pointless IMO.

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