If you haven't contacted a dynamic language, it will be a magical and weird feeling to understand JavaScript in a compiled language, because the things that are often impossible in consciousness happen, and even feel unreasonable. If you're learning JavaScript in a free and changing language, this is the way you feel. , then from the beginning, please put down your "prejudice", because this is absolutely a new continent for you. Well, not to everyone chit chat when, to get to the bottom of it, let's talk about the definition of apply and call method.
The details are as follows:
1, method definition
Call, apply all belong to a Function.prototype method, which is implemented within the JavaScript engine because it belongs to Function.prototype, so each function object instance, each method has a call , the Apply property. Since they are used as a property of a method, their use is, of course, for the method. These two methods are easy to confuse because they work just the same way.
Call Method:
Syntax: Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Definition: Invokes one method of an object, replacing the current object with another object.
Description
The call method can be used to invoke a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by Thisobj.
If the thisobj parameter is not supplied, the Global object is used as a thisobj.
Apply method:
Syntax: Apply ([Thisobj[,argarray]])
Definition: Applies one method of an object and replaces the current object with another object.
Description
If Argarray is not a valid array or is not a arguments object, it will result in a typeerror.
If none of the Argarray and Thisobj parameters are provided, the Global object will be used as a thisobj and cannot be passed any arguments
Call, apply function is to borrow other people's method to invoke, just like call oneself of the same.
The difference between them:
Apply: You can have up to two parameters-the new this object and an array Argarray. If you pass multiple arguments to the method, the arguments are written into the array, and of course, even if there is only one argument, it is written into the array. If Argarray is not a valid array or is not a arguments object, it will result in a typeerror. If you do not supply any of the Argarray and thisobj parameters, the Global object will be used as a thisobj and cannot be passed any parameters.
Call: is a direct parameter list, mainly used in the JS object when each method calls each other, so that the current instance pointer is consistent, or in special circumstances need to change the this pointer. If the thisobj parameter is not supplied, the Global object is used as a thisobj.
To put it simply, the apply and call functions are just a different form of the incoming argument list: the Func.call (FUNC1,VAR1,VAR2,VAR3) corresponding Apply is:
Func.apply (Func1,[var1,var2,var3])
Such as:
Add.apply (sub,[3,1]);
Add.call (sub,3,1);
var a={
N:1,
m:2,
add:function () {return
this.n+this.m;
}
}
var b={n:3,m:4
}
Console.log (A.add.call (b));//b.n+b.m=7
function Animal () {
this.name = "Animal ";
This.showname = function () {
alert (this.name);
}
}
function Cat () {
this.name = "Cat";
}
var animal = new Animal ();
var cat = new Cat ();
Using the call or Apply method, the ShowName () method originally belonging to the animal object is given to Object cat.
//input result is "cat"
Animal.showName.call (Cat, ",");
Animal.showName.apply (cat,[]);
The above content is a small series to introduce the Apply and call method definition and apply and call method of difference, I hope you like.