【发布时间】:2018-06-30 04:32:52
【问题描述】:
箭头函数有一个绑定的词法范围。所以在这段代码中person.person1.sayName() 应该输出HB,但它输出GOT(全局范围)。为什么会这样?
var name = "GOT";
var person = {
name : "HB",
sayName : function() { return this.name },
person1 : {
name : "WW",
sayName : () => this.name
}
};
console.log(person.person1.sayName());
【问题讨论】:
标签: javascript ecmascript-6 scope arrow-functions