Call (Thisobj [, Arg1 [, arg2 [, [, ArgN]]])
Call () Method: The official introduction is to invoke one method of an object, replacing the current object with another object.
The call () method is applied to a function object and can be used in place of another object to invoke a method that changes the object context of a function from the initial context to the new object specified by Thisobj. If the Thisobj parameter is not provided, then the global object is used as the thisobj.
It is difficult to understand the words directly. Let's look at an example:
function Class1 () { this. Name = ' fine '; This function () { Console.log (this. Name); Console.log (this); }; Console.log (this); }
Instantiate constructor varnew Class1 (); Class1.showname ();
The result of testing in Chrome is:
You can see that the values of the two console.log (this) are the same, pointing to the constructor;
And then we'll modify the example above:
functionClass1 () { This. Name = ' fine '; This. ShowName =function() {Console.log ( This. Name); Console.log ( This); }; Console.log ( This); } functionClass2 () { This. Name = ' OK '; } //create the appropriate instance varClass1 =NewClass1 (); varClass2 =NewClass2 (); Class1.showName.call (class2);//the call method must be applied to the function object
Test the following in the browser:
The first Class1 object is Console.log (this) that is located outside the ShowName method in the Class1 constructor, after the call method is used,
Class1.showName.call (Class2);
This, which is located outside the ShowName method, still points to Class1.
The next OK and Class2 objects are located inside the ShowName method, and this point has been changed to point to the Class2 constructor. As can be seen, the call method changes the direction of the this pointer in the function it applies to.
For the time being the call method understanding is this, later has the other understanding to fill up, may also have the wrong place welcome to add ~ ~
Reference to Bparadise's blog post: http://www.cnblogs.com/wuyuetian/p/4999723.html
Understanding of the call () method in JavaScript