SoFunction
Updated on 2025-03-01

The difference between JavaScript learning call and apply

1、call
call method
Calls a method of an object to replace the current object with another object.
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
parameter
thisObj
Optional. The object that will be used as the current object.
arg1, arg2, , argN
Optional. The sequence of method parameters will be passed.
illustrate
The call method can be used to call a method instead of another object. The call method changes the object context of a function from the initial context to a new object specified by thisObj.
If the thisObj parameter is not provided, the Global object is used as thisObj.
Simple column subs (function call):
Copy the codeThe code is as follows:

function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
(sub,3,1);

In this example, the meaning is to replace sub with add, (sub,3,1) == add(3,1) , so the run result is: alert(4); Example of complex points (method call):
Copy the codeThe code is as follows:

function Class1()
{
= "class1";
= function()
{
alert();
}
}
function Class2()
{
= "class2";
}
var c1 = new Class1();
var c2 = new Class2();
(c2);

Note that call means putting the c1 method on c2 to execute. It turns out that c2 does not have the showNam() method. Now, the showNam() method on c1 is putting the showNam() method on c2 to execute, so it should be class2. The result of execution is: alert("class2");
Realize inheritance
Copy the codeThe code is as follows:

function Class1()
{
= function(txt)
{
alert(txt);
}
}
function Class2()
{
(this);
}
var c2 = new Class2();
("cc");

In this way, Class2 inherits Class1. (this) means using Class1 object instead of this object. Then, don’t there all the properties and methods of Class1 in Class2? The c2 object can directly call Class1’s methods and properties. The execution result is: alert("cc");
Right, that's it. This is how JavaScript simulates inheritance in object-oriented, and can also implement multiple inheritance.

Multiple inheritance

Copy the codeThe code is as follows:

function Class10()
{
= function(a,b)
{
alert(a-b);
}
}
function Class11()
{
= function(a,b)
{
alert(a+b);
}
}

function Class2()
{
(this);
(this);
}

It's very simple, using two calls to achieve multiple inheritance
Of course, there are other methods for inheriting js, such as using prototype chains, which does not fall into the scope of this article, but only explains the usage of call
After talking about call, of course, apply, these two methods basically mean the same
The difference is that the second parameter of call can be of any type, while the second parameter of apply must be an array

2、apply

The two functions are the same for apply and call, but there are differences in parameters between the two.
The meaning of the first parameter is the same, but for the second parameter:
What applies is passed in an array of parameters, that is, multiple parameters are combined into an array, and the call is passed in as a parameter of the call (starting from the second parameter).
For example, the corresponding apply writing method is: (func1,var1,var2,var3)

Tips (elegant code and high execution efficiency)
Copy the codeThe code is as follows:

alert((5,8)) //8
alert((5,7,9,3,1,6)) //9

var arr=[5,7,9,1]
alert((null,arr));