Apply() Method
Thu Dec 28 2023 07:07:13 GMT+0000 (Coordinated Universal Time)
Saved by
@Marcelluki
The apply() Method
apply() and call() are actually pretty similar. Both methods explicitly define the value of this, but they have different ways of taking arguments. For both methods, the first parameter will be the object that we want to be the value of this. However, instead of accepting an endless number of arguments, the second parameter of apply() will be an array containing all the arguments we wish to pass to the function:
Copy code
JAVASCRIPT
const car = {
registrationNumber: 'O287AE',
brand: 'Tesla'
};
function displayDetails(greeting, ownerName) {
console.log(`${greeting} ${ownerName}`);
console.log(`Car info: ${this.registrationNumber} ${this.brand}`);
}
displayDetails.apply(car, ['Hello', 'Matt']);
/*
Hello Matt
Car info: O287AE Tesla
*/
content_copyCOPY
https://tripleten.com/trainer/web/lesson/f711442d-b2dd-4154-8776-ce7783ffba7a/task/e9c59c51-5e72-4036-ba17-7c41d7ef0a76/
Comments