With the apply()
method, you can write a method that can be used on different objects.
The apply()
method is similar to the call()
method (previous chapter).
In this example the fullName method of person is applied on person1:
const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
const person1 = {
firstName: "Mary",
lastName: "Doe"
}
// This will return "Mary Doe":
person.fullName.apply(person1);
The difference is:
The call()
method takes arguments separately.
The apply()
method takes arguments as an array.
The apply() method is very handy if you want to use an array instead of an argument list.
The apply()
method accepts arguments in an array:
const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.apply(person1, ["Oslo", "Norway"]);
Compared with the call()
method:
const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
person.fullName.call(person1, "Oslo", "Norway");
You can find the largest number (in a list of numbers) using the Math.max()
method:
Since JavaScript arrays do not have a max() method, you can apply the Math.max()
method instead.
The first argument (null) does not matter. It is not used in this example.
These examples will give the same result:
In JavaScript strict mode, if the first argument of the apply()
method is not an object, it becomes the owner (object) of the invoked function. In "non-strict" mode, it becomes the global object.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!