Summary
Function. Prototype. Apply (thisarg, argarray );
Function. Prototype. Call (thisarg [, arg1 [, arg2…]);
<SCRIPT type = "text/JavaScript">
Function func1 (){
This. P = "func1 -";
This. A = function (ARG ){
Alert (this. P + Arg );
}
}
// Define a function func2 with the property P and method B
Function func2 (){
This. P = "func2 -";
This. B = function (ARG ){
Alert (this. P + Arg );
}
}
VaR obj1 = new func1 ();
VaR obj2 = new func2 ();
Obj1.a. Apply (obj2, ["bya"]); // displays how to call the method (Object Name, parameter) in obj1)
</SCRIPT>
After Method A of obj1 is bound to obj2 for running, the runtime environment of function a is transferred to obj2,That is, the this Pointer Points to obj2.
Bytes ----------------------------------------------------------------------------------------------------------------------
Apply, call, and length attributes of the Function
Javascript defines two methods for function objects: Apply and call. They are used to bind the function to another object for running. The two methods are different only when defining parameters:
Function. Prototype. Apply (thisarg, argarray );
Function. Prototype. Call (thisarg [, arg1 [, arg2…]);
From the function prototype, we can see that the first parameter is named thisarg, that is, the this pointer inside all functions will be assigned thisarg, this achieves the purpose of running a function as another object. The two methods except the thisarg parameter are the parameters passed for the function object. The following Code Describes how the apply and call methods work:
// Define a function func1 with the property P and method
Function func1 (){
This. P = "func1 -";
This. A = function (ARG ){
Alert (this. P + Arg );
}
}
// Define a function func2 with the property P and method B
Function func2 (){
This. P = "func2 -";
This. B = function (ARG ){
Alert (this. P + Arg );
}
}
VaR obj1 = new func1 ();
VaR obj2 = new func2 ();
Obj1.a ("bya"); // display func1-byA
Obj2. B ("Byb"); // display func2-byB
Obj1.a. Apply (obj2, ["bya"]); // display the func2-byA, where ["bya"] is an array with only one element, the same below
Call through the obj1. object
Obj2. B. Apply (obj1, ["Byb"]); // display func1-byB
Obj1.a. Call (obj2, "bya"); // display func2-byA
Obj2. B. Call (obj1, "Byb"); // display func1-byB
It can be seen that after method A of obj1 is bound to obj2 for running, the runtime environment of function a is transferred to obj2, That is, the this Pointer Points to obj2. Similarly, function B of obj2 can be bound to the obj1 object for running. The last four lines of the Code show the differences between the parameters of the apply and call functions.
Different from the Length attribute of arguments, the function object also has an attribute length, which indicates the number of parameters specified during Function Definition, rather than the number of parameters actually passed during the call. For example, the following code shows 2:
Function sum (a, B ){
Return A + B;
}
Alert (sum. Length );