Recently, when using the $.each method of jquery, it occurred to me that $.each ($ (' div '), function (index,entity) {}), which came out of this index and entity, and was dispensable, And this big can tell us the subscript and the instance of the current traversal. So look at the jquery source code, that's what it says:
Debugging time to go is marked red this code, and then used the Callback.call this function, then turned to read the "JS Advanced Programming", which has a relatively deep interpretation.
First, the function is a pointer to a function object, and the name of the functions is a pointers to functions. Then in the function body, there will be a scope, that is the this keyword.
The This keyword refers to the scope of the function run, for example,
<script type= "Text/javascript" > function Funca () { alert (this); Alert ("Function A"); } </script>
The function Funca in the above code is defined in the global environment, then this is the Window object in the function body.
The following is the description of call and apply. Taking the call function as an example, the first parameter of call is to change the scope of the function, and the parameters that follow are the parameters required to pass in the function, which must always be with the parameters of the original function, for example:
<script type= "Text/javascript" > var testO = {name: "Lily" }; function Funca (A, b) { alert (this); Alert ("Function A"); } function FUNCB (A, b) { funca.call (TestO, A, b); } (FUNCB); // this becomes the Testo </script>
We defined the FUNCB function, called the call function of the Funca, and this time we changed the direction of this in Funca, which pointed to the window, and now points to the first parameter, the Testo object. and call, because the Funca function has two parameters, so if you want to Funca pass parameters, you must one by one to indicate the parameters, that is, two parameters A and B after, or you can wear only the first parameter
namely: Funca.call (TestO), or only a, namely: Funca.call (Testo,a);
The only difference between apply and call is that the second argument of apply can be in the form of an array, without having to one by one indicate the parameter, funca.apply (Testo,[a,b])
After introducing the basic usage of call and apply, it is said that the real use of his brother, the expansion function depends on the scope of the operation.
<script type= "Text/javascript" >Window.color= "Transparent"; varTestobj = {color: "Red" }; functionTestfuc () {alert ( This. color); } $(function() {1.testFuc ();//popup "Transparent"2.TESTFUC ( This);//pop Up "undefined"3.testfuc.call ( This. parent);//popup "Transparent"4.testfuc.call (window);//popup "Transparent"5.testfuc.call (Testobj);//eject "Red" });</script>
The above code demonstrates the role of call. The first function call, this points to window, so pops the window's Color property.
The second function may have some friends who think it will also pop up transparent, but first make sure our function runs in $ (function () {}); In this jquery function, the friends who understand jquery are very clear
$ (function () {}); The scope of this is the document, then we call TestFunc, and the color of the document pops up, which is undefined.
The third function points TestFunc's this to the parent window of document, and the color of the popup window is certainly not a problem.
The fourth function is more straightforward, and the window is passed in.
The fifth function, which points the testfunc this to testobj, pops up red.
In this case, the usage should be understood, but specifically how to understand how to use or to see their own routines.
I understand that this usage can be seen as a generic method in C # or Java. Like the definition of a C # method
Public void Test<t> (t A, T b) {}
This allows us to extend the method to achieve a common purpose.
The above are my own views and views, what is wrong and please point out to learn together.
The Apply method of function in JS and the call method understanding