【发布时间】:2016-09-03 16:12:05
【问题描述】:
现在我的助手正在使用function()
updateCVId: function() {
return this._id; //return the real id
}
它工作正常,但如果我使用()=>
updateCVId:()=> {
return this._id; //return undefined
}
那么 this._id 是未定义的。
事件也是如此:
'click .foo': function(evt, tmp) {
console.log(this._id); //log the real id
}
和
'click .foo': (evt, tmp)=> {
console.log(this._id); //log undefined
}
谁能告诉我如果我使用()=>,如何获取数据?
谢谢你们。
【问题讨论】:
-
在箭头函数中,this 被指定为创建函数的执行上下文的this。在函数声明或表达式中,它由调用(或 bind)设置,因此很可能是不同的对象。
-
好的,那么我在普通函数中如何获取箭头函数中的数据?
-
使用函数声明或表达式(第一个例子),见Do ES6 arrow functions always close over “this”?。
标签: javascript meteor ecmascript-6