The example analyzes the call () and apply () methods in javascript, and the example analyzes the apply
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.
Note:
The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial 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: apply a method of an object and replace the current object with another object.
Note:
If argArray is not a valid array or an arguments object, a TypeError occurs.
If neither argArray nor thisObj is provided, the Global object will be used as thisObj and cannot be passed with any parameters.
2. Common instances
A,
Copy codeThe Code is as follows:
Function add (a, B)
{
Alert (a + B );
}
Function sub (a, B)
{
Alert (a-B );
}
Add. call (sub, 3, 1 );
In this example, we use add to replace sub and add. call (sub, 3, 1) = add (3, 1), so the running result is: alert (4); // Note: Functions in js are actually objects, the Function name is a reference to the Function object.
B,
Copy codeThe Code 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 ();
// Use the call or apply method to send the showName () method of the original Animal object to the cat object.
// The input result is "Cat"
Animal. showName. call (cat ,",");
// Animal. showName. apply (cat, []);
Call means to put the animal Method on cat for execution. In the past, cat didn't have the showName () method. Now it is to put the showName () method of animal on cat for execution, so this. the name should be Cat
C. Implement inheritance
Copy codeThe Code 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 that the Animal object is used to replace this object, so does Cat have all the attributes and methods of Animal, the Cat object can directly call the Animal method and attributes.
D. Multi-Inheritance
Copy codeThe Code is as follows:
Function Class10 ()
{
This. showSub = function (a, B)
{
Alert (a-B );
}
}
Function Class11 ()
{
This. showAdd = function (a, B)
{
Alert (a + B );
}
}
Function Class2 ()
{
Class10.call (this );
Class11.call (this );
}
It's easy to implement multi-inheritance with two calls.
Of course, there are other methods to inherit js, such as using prototype chain. This does not belong to the scope of this article, but only describes the usage of call here. When we talk about call and apply, the two methods basically mean that the second parameter of call can be of any type, and the second parameter of apply must be an array, it can also be arguments.
Callee, caller ..
Summary:
To put it simply, the two methods have the same effect.
Differences: parameters passed by methods are different.