Common:
Function: Invokes one method of an object, replacing the current object with another object. Changes the object context of a function from the initial context to the new object specified by Thisobj. If the Thisobj parameter is not provided, then the Global object is used as the thisobj.
Take call for example, Syntax: Obj1.method1.call (OBJ2,ARGUMENT1,ARGUMENT2)
To give a specific 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 the sub with Add, Add.call (sub,3,1) = = Add (3,1), so the result is: alert (4); Note: The function in JS is actually an object, and 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 Shownam () method, is now C1 the Shownam () method put to C2 to execute, so this.name should be class2, the result of execution is: alert (" Class2 ");
Inherited
function Class1 ()
{
This.showtxt = function (TXT)
{
alert (TXT);
}
}
function Class2 ()
{
Class1.call (this);
}
var C2 = new Class2 ();
C2.showtxt ("CC");
So Class2 inherit Class1, Class1.call (this) means to use the Class1 object instead of the This object, then Class2 all the properties and methods Class1, C2 object can directly call Class1 method to and attributes, the result of execution is: alert ("CC");
Different points:
Call the second parameter is the argument list
Apply the second argument to an array or arguments object
The similarities and differences between call and apply