Calling a Function Using function.apply
Another way to invoke functions in JavaScript is using the .apply()
method. The syntax for .apply()
is as follows: functionName.apply(thisArg, [array])
. "functionName
" refers to the function being called. However, with .apply()
, an array containing all values can be sent instead of individual arguments. Additionally, "thisArg
" specifies the object that "this" references inside the invoked function.
Syntax
apply(thisArg) apply(thisArg, argsArray)
Parameters
thisArg
-
The value of
this
provided for the call tofunc
. If the function is not in strict mode,null
andundefined
will be replaced with the global object, and primitive values will be converted to objects. argsArray
Optional-
An array-like object, specifying the arguments with which
func
should be called, ornull
orundefined
if no arguments should be provided to the function.
Return value
The result of calling the function with the specified this
value and arguments.