What's the difference between '$(this)' and 'this'?

前端 未结 7 1582
面向向阳花
面向向阳花 2020-11-21 07:18

I am currently working through this tutorial: Getting Started with jQuery

For the two examples below:

$(\"#orderedlist\").find(\"li\").each(function          


        
7条回答
  •  别那么骄傲
    2020-11-21 08:00

    this is the element, $(this) is the jQuery object constructed with that element

    $(".class").each(function(){
     //the iterations current html element 
     //the classic JavaScript API is exposed here (such as .innerHTML and .appendChild)
     var HTMLElement = this;
    
     //the current HTML element is passed to the jQuery constructor
     //the jQuery API is exposed here (such as .html() and .append())
     var jQueryObject = $(this);
    });
    

    A deeper look

    thisMDN is contained in an execution context

    The scope refers to the current Execution ContextECMA. In order to understand this, it is important to understand the way execution contexts operate in JavaScript.

    execution contexts bind this

    When control enters an execution context (code is being executed in that scope) the environment for variables are setup (Lexical and Variable Environments - essentially this sets up an area for variables to enter which were already accessible, and an area for local variables to be stored), and the binding of this occurs.

    jQuery binds this

    Execution contexts form a logical stack. The result is that contexts deeper in the stack have access to previous variables, but their bindings may have been altered. Every time jQuery calls a callback function, it alters the this binding by using applyMDN.

    callback.apply( obj[ i ] )//where obj[i] is the current element
    

    The result of calling apply is that inside of jQuery callback functions, this refers to the current element being used by the callback function.

    For example, in .each, the callback function commonly used allows for .each(function(index,element){/*scope*/}). In that scope, this == element is true.

    jQuery callbacks use the apply function to bind the function being called with the current element. This element comes from the jQuery object's element array. Each jQuery object constructed contains an array of elements which match the selectorjQuery API that was used to instantiate the jQuery object.

    $(selector) calls the jQuery function (remember that $ is a reference to jQuery, code: window.jQuery = window.$ = jQuery;). Internally, the jQuery function instantiates a function object. So while it may not be immediately obvious, using $() internally uses new jQuery(). Part of the construction of this jQuery object is to find all matches of the selector. The constructor will also accept html strings and elements. When you pass this to the jQuery constructor, you are passing the current element for a jQuery object to be constructed with. The jQuery object then contains an array-like structure of the DOM elements matching the selector (or just the single element in the case of this).

    Once the jQuery object is constructed, the jQuery API is now exposed. When a jQuery api function is called, it will internally iterate over this array-like structure. For each item in the array, it calls the callback function for the api, binding the callback's this to the current element. This call can be seen in the code snippet above where obj is the array-like structure, and i is the iterator used for the position in the array of the current element.

提交回复
热议问题