函数需要一个具有
我需要帮助解决这个问题;
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));
firstname
和named
属性的对象。
实现您想要的(不更改该函数(的唯一方法是创建一个包含它们的新对象,并将其传递给您提到的某个函数。
console.log(person.dog.owner.call({ firstname: "Benjamin", named: "Louie" }));