In ECMAScript V3, the two methods are defined for the function stereotype, and the two methods work the same way: using these two methods to invoke functions like other object methods is copied from the book, at least I didn't understand what it meant.
The following is easy to understand, first look at the code:
Copy Code code as follows:
function introduce (name,age)
{
document.write ("My name is" +name+.) I am "+age);
}
var p=new people ();
Introduce.call (P, "windking", 20);
Just say this piece of code above, use call, Introduce became P method, do not know this to say you understand? Using the call method, the code above is equivalent to this code:
Copy Code code 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 is also the same effect.
Well, we don't care what this method can actually do in practice, let's talk about grammar first.
Call accepts at least one parameter, and the first parameter of the invocation refers to the object you need, such as the example above, where the introduce method wants him to be called by the object P, and then the p as the first parameter of calling. The number of remaining parameters is arbitrary and acts as a parameter to the introduce method. In the order that the order is declared by the introduce parameter. For example Introduce.call (p, "windking", 20), if introduce is an instance method of P, then that is: P. Introduce ("Windking", 20). You get it? Keep in mind that the order of incoming arguments is consistent with the order in which the function declares the parameters.
Understanding the Call,apply method is easy to understand, the only difference between apply and call is that call accepts at least one parameter, and apply accepts only two parameters, the first parameter is the same as call, and the second parameter is a collection of subscript, such as Introduce.call (p, "windking", 20) can be rewritten as introduce.apply (p,["windking", 20)). Do you get it this time?
So what is the use of these two methods? Wouldn't it be better to implement introduce as people if we're just trying to achieve the above function?
I summed up the application to two:
1. Sharing methods. First look at the code:
Copy Code code as follows:
function introduce (name,age)
{
document.write ("My name is" +name+.) I am "+age);
}
This is a self-introduction method, now suppose we have a boy's class, and a girl's class (here I just want to demo, in practice, will use a people parent), because their introduce are the same, so we can share this method.
Copy Code code as follows:
function Boy ()
{
This. Boyintroduce=function () {
Introduce.call (This,name,age);
};
}
Similarly, girl is the same, so that we can avoid writing code. In fact, this is a bit far-fetched, because we can write in full:
Copy Code code as follows:
function Boy ()
{
This. Boyintroduce=function () {
Introduce (name,age);
}
}
But this time, if we use apply, it looks a lot simpler:
Copy Code code as follows:
function Boy ()
{
This. Boyintroduce=function () {
Introduce.apply (this,arguments);
};
}
Is it a lot simpler? If there are many parameters, then there is no need to write a string of dense parameters!
2. Cross-domain calls
See a simple example (demo only, no value):
Copy Code code 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's no objection. Suppose one day a girl also want to do a self-introduction, but occasionally use, then I do not need to modify the girl class, because the other girls are more shy, do not like to introduce themselves. Then I can do it this time.
var g=new Girl ("Xuan", 22);
Introduce.call (g, "Xuan", 22);
3. Real use--inheritance
Well, above all are tricks, do not ascend, the following is called and apply the most extensive application, is used to construct inheritance.