函数方法.apply().call().bind()



我需要帮助解决这个问题;

let person = {
firstname: "Benjamin",
dog: {
named: "Louie",
owner: function() {
return this.named + " is " + this.firstname + "'s dog'";
}
}
}
console.log(person.dog.owner.call(person)); // prints undefined is Benjamin's dog' instead of Louie is Benjamin's dog'

我知道call((方法将引用没有name属性的person对象。

有没有办法使用bind((call((或apply((方法打印"Louie is Benjamin's dog'"

您的named密钥位于dog之下。所以称之为this.dog.named

let person = {
firstname: "Benjamin",
dog: {
named: "Louie",
owner: function() {
return this.dog.named + " is " + this.firstname + "'s dog'";
}
}
}
console.log(person.dog.owner.call(person));

this.named应该是this.dog.named,因为named属性在dog对象内。

点击此处:

let person = {
firstname: "Benjamin",
dog: {
named: "Louie",
owner: function() {
return this.dog.named + " is " + this.firstname + "'s dog'";
}
}
}
console.log(person.dog.owner.call(person));

函数需要一个具有firstnamenamed属性的对象。

实现您想要的(不更改该函数(的唯一方法是创建一个包含它们的新对象,并将其传递给您提到的某个函数。

console.log(person.dog.owner.call({ firstname: "Benjamin", named: "Louie" }));

最新更新