use*_*159 3 javascript callback this node.js ecmascript-6
在traversenpm包中有这个例子
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
traverse(obj).forEach(function (x) {
if (x < 0) this.update(x + 128);
});
Run Code Online (Sandbox Code Playgroud)
在回调函数内部,您可以调用this.update.我知道在这种情况下你应该使用常规(不是ES6箭头)样式的函数定义,如上所示.
但出于好奇,你如何使用ES6箭头函数语法使代码工作?如果我尝试如下,我得到TypeError: Cannot read property 'update' of undefined因为当然this不同于上面.
traverse(obj).forEach((x) => {
if (x < 0) this.update(x + 128);
});
Run Code Online (Sandbox Code Playgroud)
我尝试this通过使用改变,bind但没有成功.你如何改变thisES6箭头功能?
| 归档时间: |
|
| 查看次数: |
1110 次 |
| 最近记录: |