1. Method definition
Call method:
Syntax: call([thisObj[,arg1[, arg2[, [,.argN]]]]])
Definition: Call a method of an object to replace the current object with another object.
illustrate:
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 the initial context to a new object specified by thisObj.
If thisObj parameter is not provided, the Global object is used as thisObj.
apply method:
Syntax: apply([thisObj[,argArray]])
Definition: Apply one method of a certain object and replace the current object with another object.
illustrate:
If argArray is not a valid array or is not an arguments object, a TypeError will be generated.
If no arguments are provided, the Global object will be used as thisObj and cannot be passed any arguments.
2. Common examples
a.
The code copy is as follows:
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(ab);
}
add.call(sub,3,1);
The meaning in this example is to replace sub with add, add.call(sub,3,1) == add(3,1) , so the running result is: alert(4); // Note: The function in js is actually an object, and the function name is a reference to the Function object.
b.
The code copy is as follows:
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 originally belonging to the Animal object is handed over to the object cat for use.
//The input result is "Cat"
animal.showName.call(cat,",");
//animal.showName.apply(cat,[]);
call means putting the animal method on cat to execute. It turns out that cat does not have a showName() method. Now, putting the animal showName() method on cat to execute, so this.name should be Cat
c. Realize inheritance
The code copy is as follows:
function Animal(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
function Cat(name){
Animal.call(this, name);
}
var cat = new Cat("Black Cat");
cat.showName();
Animal.call(this) means using Animal object instead of this object. Then don’t there be all the properties and methods of Animal in Cat? The Cat object can directly call Animal methods and properties.
d. Multiple inheritance
The code copy is as follows:
function Class10()
{
this.showSub = function(a,b)
{
alert(ab);
}
}
function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}
function Class2()
{
Class10.call(this);
Class11.call(this);
}
It's very simple, using two calls to achieve multiple inheritance
Of course, there are other methods for inheriting js, such as using prototype chains, which does not fall into the scope of this article, but only explains the usage of call here. Speaking of call, of course there is also apply. These two methods basically mean the same thing. The difference is that the second parameter of call can be of any type, while the second parameter of apply must be an array or arguments
And callee, caller..