Function apply() Method
Description
The apply() method calls a function with a given
this value.
The apply() method lets objects borrow methods from other objects.
Example
// Create a person Object
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
// Create a person1 Object
const person1 = {
firstName:"John",
lastName: "Doe"
}
// Apply the fulllName method of person to person1:
let name = person.fullName.apply(person1);
Try it Yourself »
The Difference Between call() and apply()
The call() method takes arguments separately.
The apply() method takes arguments as an array.
Note
The apply() method is very handy if you want to use an array instead of an argument list.
Syntax
method.apply(object, arguments)
Parameters
| Parameter | Description |
| method | Required. The method to apply from. |
| object | Required. The object to apply to. |
| arguments | Optional. Array-like object with function arguments. |
Return Value
| Type | Description |
| Value | The result of the function. |
Function Methods & Properties
Browser Support
apply() is an ECMAScript3 (JavaScript 1999) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |