【问题标题】:Why Global this not accesible inside object? [duplicate]为什么 Global this 在对象内部无法访问? [复制]
【发布时间】:2019-04-07 23:16:04
【问题描述】:

为什么全局 this 在对象内部无法访问?

name = 'Name1';

const person = {
  name: 'Name2',
  greet: () => {
    console.log('Hi, I am ' + this.name);
  }
}

person.greet();

this.name 显示undefined,应该是Name1

【问题讨论】:

标签: javascript


【解决方案1】:

这是因为使用 this()=>{} 表示法存在问题。

name = 'Name1';

const person = {
  name: 'Name2',
  greet: function () {
    console.log('Hi, I am ' + this.name);
  }
}

person.greet();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2014-11-07
    • 1970-01-01
    • 2015-12-07
    • 2020-07-08
    相关资源
    最近更新 更多