Use of the javascript Call method, javascriptcall
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
Function Animal (){
This. name = "Animal ";
This. showName = function (){
Alert (this. name );
};
}
Function Cat (){
This. name = "Cat ";
}
Var animal = new Animal ();
Var cat = new Cat ();
Animal. showName. call (cat ,",");
Call problems in javascript
The call and apply methods are mainly used to modify the this pointer during function running.
Modify your code to see the difference:
Var each = function (array, fn) {for (var index in array) {// use the // fn (index, array [index]) format or // fn here. call (null, index, array [index]) or // fn. apply (null, index, array [index]) // this inside the function points to window // and when you want to modify this, we can see the difference between call and apply and direct brackets. call (array [index], index, array [index]) ;}}; // you can use this to obtain the element each ([4, 20, 20, 3], function () {alert (this) ;}); // There is no difference between each ([4, 20, 3], function (index, elem) {alert (elem );});
URL: www.w3school.com.cn/..ng.asp
Understanding of call functions in javascript
Standard interpretation: The call method is to execute an object's method in the context of another object.
In your example, myFun. call (myObject, 13, 3, 95) is executed like this:
1. Call the myFun Function
2. Pass the three parameters 13, 3, and 95 to it (your myFun method does not have code to process these parameters, so it is useless to pass them)
3. replace all this in the myFun function with myObject (similarly, your myFun function does not use this, so the execution result is the same as calling myFun () directly)
Modify your example to help you understand:
Function myFun (p1, p2, p3)
{
This. para1 = p1;
This. para2 = p2;
This. para3 = p3;
}
Var myObject = new Object ();
Alert (myObject. para1) // display undefined
MyFun. call (myObject, 95);/* during execution, this in the myFun method is all replaced by the myObject object, so in this example, the execution of the myFun method is the same as that of the directly Written Statement:
MyObject. para1 = 13;
MyObject. para2 = 3;
MyObject. para3 = 95;
*/
Alert (myObject. para1) // display 13