javascript - 函数方法 .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() 方法将引用不具有属性的人员对象 - 命名。
有没有办法使用 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));
推荐阅读
- html - 如何在同一个div中将文本分成两个单独的部分
- google-sheets - 将 Googlesheet 数据上传到 Google Cloud 服务,然后使用 CloudSQL 进行分析
- python - 如何将打印精美的数据框读入 Pandas 数据框?
- python - 在列表python中的列表中添加列表中的元素
- node.js - fulent-ffmpeg 参数输入无效 | 节点
- reactjs - VSCode jsx 表达式格式化
- javascript - Javascript标签被忽略?
- reactjs - 服务器上收到的值与 POST 请求中发送的值不同
- regex - 如何重写现有的重写路径?
- excel - 有没有办法根据条件初始化公共数组?