【问题标题】:How to display the field of the parent object如何显示父对象的字段
【发布时间】:2022-11-18 15:09:15
【问题描述】:
const human = {
    name: "asdasd",
    mouth: {
        sayName(){
            console.log(this.name)
        }
    }
}

human.mouth.sayName()
// undefined

你好! 我需要从父对象中获取名称字段 谢谢你!

【问题讨论】:

  • 它不能以这种方式完成,因为对象之间没有连接。尝试编辑问题并解释您要解决的问题是什么——您可能会在不起作用的解决方案旁边发现新的解决方案。
  • this 更改为human,但我不知道这是否是您想要的。

标签: javascript


【解决方案1】:

你可以试试这个,

const human = {
  name: "asdasd",
  mouth: {
    sayName() {
      return human.name;
    }
  }
};
console.log(human.mouth.sayName());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2019-09-17
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多