请问您能告诉我什么是snippet 1的错误?我对obj.discover()的期望输出是3.我怎样才能将this
绑定到对象的箭头函数方法?对箭头功能感到困惑
'use strict'
// Snippet 1
var obj = {
data: 3,
discover:() => {
return this.data
}
}
obj.discover() // -> undefined
obj.discover.bind(obj)() // undefined
// but if I don't use the arrow notation, everything works
// Snippet 2
var obj2 = {
data: 3,
discover: function(){
return this.data
}
}
obj2.discover() // -> 3
箭头函数没有自己的'this',使用该函数的常规函数 –