In ECMAScript v3, these two methods are defined for the Function prototype. The functions of these two methods are the same: using these two methods can call functions like calling other object methods. This sentence was copied from the book, at least I didn't understand what this means.
Let’s talk about simple and easy to understand. Let’s first look at the code:
The code copy is as follows:
function Introduction(name,age)
{
document.write("My name is "+name+".I am "+age);
}
var p=new People();
Introduction.call(p,"Windking",20);
Let’s talk about the above code. After using call, Introduction becomes the method of p. I wonder if you understand this? Using the call method, the above code is equivalent to this code:
The code copy is as follows:
function People(name,age)
{
this.name=name;
this.age=age;
this.Introduce=function(){
document.write("My name is "+name+".I am "+age);
};
}
Do you understand the meaning? apply the same function.
OK, no matter what this method can be used in practice, let’s talk about the grammar first.
The call accepts at least one parameter. The first parameter of the call refers to the object you need. For example, in the example above, the Introduction method hopes that it can be called by object p, so p is used as the first parameter of the call. The remaining number of parameters is arbitrary, and it is used as a parameter of the Introduction method. The order is in the order declared by the Introduce parameter. For example, Introduce.call(p,"Windking",20), if Introduce is an example method of p, then this is what it means: p.Introduce("Windking",20). Do you understand? Remember that the order of incoming parameters must be consistent with the order of function declaration parameters.
After understanding the call, the apply method is easy to understand. The only difference between apply and call is that call accepts at least one parameter, while apply only accepts two parameters. The first parameter is the same as call, and the second parameter is a set with subscripts. For example, Introduction.call(p,"Windking",20) can be rewritten into Introduction.apply(p,["Windking",20]). Do you understand this time?
So what are the uses of these two methods? If we just want to implement the above function, isn't it better to implement the Introduction as People?
I summarize the application into two:
1. Sharing method. Let's look at the code first:
The code copy is as follows:
function Introduction(name,age)
{
document.write("My name is "+name+".I am "+age);
}
This is a self-introduction method. Now let's say we have a boy's class and a girl's class (I'm just for demonstration here, in practice, a People's parent class will be used), because their introduction is the same, so we can share this method.
The code copy is as follows:
function Boy()
{
this.BoyIntroduce=function(){
Introduction.call(this,name,age);
};
}
Similarly, the same is true in Girl, so we can avoid writing code. Actually, this is a bit far-fetched, because we can write it as:
The code copy is as follows:
function Boy()
{
this.BoyIntroduce=function(){
Introduction(name,age);
}
}
But at this time, if we use Apply, it will look much simpler:
The code copy is as follows:
function Boy()
{
this.BoyIntroduce=function(){
Introduction.apply(this,arguments);
};
}
Isn't it much simpler? If there are many parameters, then don’t you need to write such a series of dense parameters!
2. Cross-domain call
See a simple example (for demonstration only, no value):
The code copy is as follows:
function Boy(name,age)
{
this.BoyIntroduce=function(){
document.write("My name is "+name+".I am "+age);
}
}
function Girl(name,age)
{
}
This is a Boy and a Girl class, and then we write the following code:
var b=new Boy("Windking",20);
b.BoyIntroduce();
There is no objection to this. Suppose there is a girl who wants to introduce herself one day and just use it by chance, then there is no need for me to modify the Girl class, because other girls are shy and don’t like to introduce themselves. Then I can do this at this time.
var g=new Girl("Xuan",22);
Introduction.call(g,"Xuan",22);
3. Real use - inheritance
Okay, the above are all small tricks, and it is not a place of elegance. Below is the most widely used call and apply, which is used for structural inheritance.