Function.prototype.call()
The call() method calls the function with a given this value and arguments provided individually.
Syntax
call(thisArg)
call(thisArg, arg1)
call(thisArg, arg1, /* …, */ argN)
Parameters
thisArg- : The value to use as
thiswhen callingfunc. If the function is not in strict mode,nullandundefinedwill be replaced with the global object, and primitive values will be converted to objects.
- : The value to use as
arg1, …, argNoptional- : Arguments for the function.
Return value
The result of calling the function with the specified this value and arguments.
Description
Note: This function is almost identical to
Function.prototype.apply(), except thatcall()accepts an argument list, whileapply()accepts a single array of arguments — for example,func.apply(this, ['eat', 'bananas'])vs.func.call(this, 'eat', 'bananas').
Normally, when calling a function, the value of this inside the function is the object that the function was accessed on. With call(), you can assign an arbitrary value as this when calling an existing function, without first attaching the function to the object as a property. This allows you to use methods of one object as generic utility functions.
Warning: Do not use
call()to chain constructors (for example, to implement inheritance). This invokes the constructor function as a plain function, which meansnew.targetisundefined, and classes throw an error because they can't be called withoutnew. UseReflect.construct()orextendsinstead.