Home>

The apply and call functions are the same,Just the form of the parameter list passed in is different,This article introduces the definition of apply and call methods and the difference between apply and call methods.Interested friends study together

If you have not been exposed to dynamic languages,Understanding JavaScript in a compiled language way will have a magical and weird feeling.Because consciously often impossible things happen,Even feel ridiculous.If you encounter this feeling while learning the free and ever-changing language of javascript,So from now on,Please put down your "bias" because it is definitely a new continent for you.Okay, I wo n’t give you a chat.Closer to home,Let me first explain the definition of the apply and call methods.

The details are as follows:

1.Method definition

call, apply are both methods of function.prototype,It is implemented internally by the javascript engine,Because it belongs to function.prototype, each function object instance,That is, each method has call and apply attributes. Since it is a method attribute,Then their use is of course directed at methods.These two methods are easily confused,Because they work the same,Just use it differently.

call method:

Syntax:call ([thisobj [, arg1 [, arg2 [, [, .argn]]]]])

Definition:call a method of an object,Replace the current object with another object.

Explanation:

The call method can be used to call a method on behalf of another object.The call method changes the object context of a function from the original context to the new object specified by thisobj.

If the thisobj parameter is not provided, the global object is used as thisobj.

apply method:

Syntax:apply ([thisobj [, argarray]])

Definition:a method of applying an object,Replace the current object with another object.

Explanation:

If argarray is not a valid array or an arguments object, a typeerror will result.

If neither argarray nor thisobj is provided,Then the global object will be used as thisobj and cannot be passed any parameters

call, apply is to use someone else's method to call,It's like calling itself.

What makes them different:

apply:There can be at most two parameters-a new this object and an array argarray. If you pass multiple parameters to the method,Write the parameters into this array,Of course, even if there is only one parameter,Also write into the array.If argarray is not a valid array or an arguments object, a typeerror will result. If neither argarray nor thisobj is provided,Then the global object will be used as thisobj and cannot be passed any parameters.

call:is a direct parameter list,It is mainly used when the methods of the js object call each other.Keep the current this instance pointer consistent,Or in special cases you need to change the this pointer. If the thisobj parameter is not provided, the global object is used as thisobj.

More simply,The apply and call functions are the same,Only the form of the parameter list passed in is different:for example, func.call (func1, var1, var2, var3) corresponds to the apply form:

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 ();
//Through the call or apply method, the showname () method that originally belonged to the animal object was passed to the object cat for use.
//The input result is "cat"
animal.showname.call (cat, ",");
animal.showname.apply (cat, []);

The above is the definition of the apply and call methods and the differences between the apply and call methods.Hope you like it.