Call method
Please see
Apply to: Function Object
Requirements
Version 5.5
Invokes one of the object's methods to replace the current object with another object.
Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Parameters
Thisobj
Options available. The object that will be used as the current object.
Arg1, Arg2, argn.
Options available. A sequence of method parameters is passed.
Description
The call method can be used to invoke 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 supplied, the Global object is used as a thisobj.
-------------------------------------------------------------------------------------------
At first glance, it's easy to look at people and make some simple explanations.
Obj1.method1.call (Obj2,argument1,argument2)
As above, call's role is to put the Obj1 method on the Obj2 to use, the back of the argument1. These are introduced as parameters.
Give a concrete example.
function Add (a,b)
{
alert (A+B);
}
function sub (a,b)
{
alert (a-b);
}
Add.call (sub,3,1);
The meaning of this example is to replace sub,add.call (sub,3,1) = Add (3,1) with add, so the result is: alert (4); Note: The function in JS is actually an object, the function name is a reference to a function object.
Look at a slightly more complicated example.
function Class1 ()
{
THIS.name = "Class1";
This.shownam = function ()
{
alert (this.name);
}
}
function Class2 ()
{
THIS.name = "Class2";
}
var C1 = new Class1 ();
var C2 = new Class2 ();
C1.showNam.call (C2);
Note that call means to put the C1 method on the C2, the original C2 is not the Shownam () method, now is the C1 Shownam () method to carry out the C2, so this.name should be class2, the result of execution is: alert (" Class2 ");
Quote a piece of code on the Web, after running naturally understand its rationale.
<input type= "text" id= "MyText" value= "input text" >
<script>
function Obj () {this.value= Object! ";}
var value= "global variable";
function Fun1 () {alert (this.value);}
Window. Fun1 (); Global variables
Fun1.call (window); Global variables
Fun1.call (document.getElementById (' MyText ')); Input text
Fun1.call (New OBJ ()); Object!
</script>
The first parameter of the call function and the Apply method is the object to pass to the current object, and this within the function. Subsequent arguments are parameters that are passed to the current object.
Run the following code:
<script>
var func=new function () {this.a= "func"}
var myfunc=function (x) {
var a= "MyFunc";
alert (THIS.A);
alert (x);
}
Myfunc.call (func, "Var");
</script>
Visible Func and Var are ejected separately. Here we have an idea of the meaning of every parameter of call.
Both apply and call are the same in effect, but they differ in their parameters.
Is the same for the first parameter, but for the second argument:
Apply passes in an array of arguments, which is the combination of multiple arguments into an array, while call is passed as a call parameter (starting with the second argument).
such as Func.call (FUNC1,VAR1,VAR2,VAR3) corresponding apply is written as: Func.apply (FUNC1,[VAR1,VAR2,VAR3))