gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:26:59 25 4
gpt4 key购买 nike

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

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 key 位于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));

关于javascript - 函数方法 .apply() .call() .bind(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51325099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com