SoFunction
Updated on 2025-03-08

Difference between call and apply in JavaScript

If you have never been exposed to dynamic languages, you will have a magical and weird feeling to understand JavaScript in a compiled language way, because things that are often impossible in consciousness happen, and even feel unreasonable. If you encounter this feeling in the process of learning JavaScript, a free and endless language, then from the present form, please let go of your "prejudice", because this is definitely a new world for you, let JavaScript be allowed to do so.

OK, let's get back to the point, first understand the context characteristics of JavaScrtipt dynamically transformed runtime. This characteristic is mainly reflected in the application of the two methods of apply and call.

1. Definition of method

Call method:

Syntax: call(thisObj,Object)

Definition: Call a method of an object to replace the current object with another object.

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.

apply method:

Syntax: apply(thisObj, [argArray])

Definition: Apply one method of a certain object and replace the current object with another object.

illustrate:

If argArray is not a valid array or is not an arguments object, a TypeError will be generated.

If no arguments are provided, the Global object will be used as thisObj and cannot be passed any arguments.

--------------------------------------------------------------------------------

Note: The call and apply methods are exactly the same, but apply in the way of passing parameters, it passes parameters in an array.

Code example:

function Animal(name) {
 = name;
 = function() {
();
};
}
function Cat(name) {
(this, name);
}
 = new Animal();
function Dog(name) {
(this, name);
}
 = new Animal();
var cat = new Cat("Black Cat"); //Call must be objectvar dog = new Dog(["Black Dog"]); //apply must be array();
();
(cat instanceof Animal);
(dog instanceof Animal); 

-------------------------------------------------------------------------------

Simulate call, apply this replacement

function Animal(name) {
 = name;
 = function() {
alert();
};
};
function Cat(name) {
 = Animal;
(name);
delete superClass;
}
var cat = new Cat("Black Cat");
();

Summarize:

Their respective definitions:

apply: Apply one method of an object and replace the current object with another object.

call: Call a method of an object to replace the current object with another object.

What they have in common:

All "can be used to call a method instead of another object, changing the object context of a function from the initial context to a new object specified by thisObj." - Excerpted from JScript5.5.chm

What makes them different:

apply: There can be only two parameters at most - a new this object and an array argArray. If you pass multiple parameters to this method, then write all the parameters into this array. Of course, even if there is only one parameter, it must be written into the array. If argArray is not a valid array or is not an arguments object, a TypeError will be generated. If no arguments are provided, the Global object will be used as thisObj and cannot be passed any arguments.

call: It is a direct parameter list, mainly used when various methods of the js object are called to each other, so that the current pointer of this instance remains consistent, or in special cases, this pointer needs to be changed. If the thisObj parameter is not provided, the Global object is used as thisObj.

To put it simply, apply is the same as call function, except that the incoming parameter list form is different: For example, the corresponding apply written by (func1, var1, var2, var3) is: (func1,[var1, var2, var3])