Ext.: http://www.cnblogs.com/ArthurPatten/p/3335912.html
We can consider call and apply as a method of an object, calling the function indirectly in the form of a calling method. The first argument to call and apply is the parent object that is to invoke the function, which is the calling context through which this is used in the body of the function to obtain a reference to it.
For example, if you want to invoke the function f as a method of object o, you can use the call and apply methods as follows:
F.call (o);
F.apply (o);
The following code can be used to understand:
O.M = f; Temporary method to store F as O
O.M (); Call this temporary method
Delete o.m; Remove this temporary method
Let's take an example.
function Testfun () {
return THIS.A + this.b;
}
var o = {a:1, b:2};
Testfun.call (o); 3
Testfun.apply (o); 3
The result of the above code execution is 3, which can be understood as return O.A + O.B.
Consider a problem if the first argument of the call and the Apply method is null or undefined? Take a look at the following example:
var a = ten, B = 20;
function Testfun () {
return THIS.A + this.b;
}
Testfun.call ();
Testfun.apply ();
The result of the above code execution is 30. This is because the first argument of call and apply, if passed in null or undefined, is replaced by the global object.
What is the difference between the two methods of call and apply?
For the call method, all arguments after the first invocation of the context argument are the values to pass in the function to be called. For example, to invoke the function f in the form of the object O method, and pass in two parameters, you can use the following code:
F.call (o, 1, 2);
The Apply method places all arguments after the first argument into an array,
F.apply (O, [1, 2]);
Let's take an example.
function Testfun (x, y) {
Return THIS.A + this.b + x + y;
}
var o = {a:1, b:2};
Testfun.call (o, 10, 20);
Testfun.apply (o, [10, 20]);
The execution result of the above code is 33, which can be understood as return O.A + O.B + 10 + 20