在 ES6中,这两种方法都是合法的:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
简而言之:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
是否也可以使用新的箭头函数
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
或者
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
我得到一个错误消息,表明该方法不能访问 this
。这仅仅是一个语法问题,还是不能在 ES6对象内部使用胖箭头方法?