Definition of apply and call methods and differences between apply and call methods

  • 2020-11-03 22:02:20
  • OfStack

Without exposure to dynamic languages, understanding javaScript in a compiled-language way of thinking can feel magical and weird, because often things that are not consciously possible happen, even impossible. If you encounter this feeling in the process of learning JavaScript, a language of freedom and endless change, then from now on, please put down your "prejudice", because this is definitely a new continent for you. Ok, so let's get down to business and talk about the definition of apply and call methods.

The specific content is as follows:

1. Method definition

call, apply are one of the methods of ES13en. prototype, which is implemented internally by the JavaScript engine. Since Function. prototype, each object instance of Function has call, apply attributes for each method. Since they are properties of methods, their use is, of course, specific to methods. These two methods are confusing because they do exactly the same thing, but are used in different ways.

call method:

Grammar: call ([thisObj [arg1 [, arg2 [[. argN]]]]])

Definition: Calls 1 method on 1 object to replace the current object with another object.

Description:

The call method can be used to call a method instead of another object. The call method changes the object context of a function from its original context to a new object specified by thisObj.

If no thisObj parameter is provided, the Global object is used as thisObj.

apply method:

Grammar: apply ([thisObj [argArray]])

Definition: Apply 1 method to a 1 object and replace the current object with another object.

Description:

If argArray is not a valid array or is not an arguments object, this results in an TypeError.

If argArray and thisObj are not provided with any 1 parameter, the Global object will be used as thisObj and cannot be passed any parameter

call, apply is just a way to borrow someone else's method and call it as if it were your own.

The differences:

apply: There can be no more than two arguments -- the new this object and one array argArray. If you pass more than one argument to the method, you write all the arguments into the array, even if there is only one argument. If argArray is not a valid array or is not an arguments object, this results in an TypeError. If no 1 parameter of argArray and thisObj is provided, the Global object will be used as thisObj and cannot be passed any parameters.

call: is the direct parameter list, mainly used in js object methods call each other, so that the current this instance pointer remains 1, or in special cases need to change the this pointer. If the thisObj parameter is not provided, the Global object is used as thisObj.

More simply, apply and call function 1 sample, just the incoming parameter list different form: as func. call (func1 var1, var2, var3) corresponding apply written as follows:

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 call or apply Methods that would have belonged to Animal The object's showName() Method to object cat To use.  
// Input result is "Cat" 
animal.showName.call(cat,","); 
animal.showName.apply(cat,[]);

The above is the definition of apply and call methods and the difference between apply and call methods introduced by this site. I hope you enjoy it.


Related articles: