我刚开始改写一些代码使用ES6,但是发现箭头函数在某些代码中不起作用,我不确定原因。这些代码来自于一个用于装饰Hapi的插件,以增加reply
接口功能。
ES5:
server.decorate('reply', 'test', function(schema, response) {
return this.response(mask(schema, response));
});
ES6:
server.decorate('reply', 'test', (schema, response) => {
return this.response(mask(schema, response));
});
E66错误导致设备无法正常工作:
Uncaught error: this.response is not a function
这是为什么呢?
this
绑定到外部作用域,因此在这种情况下不等同。 - Seiyriafunction
吗? - cyberwombatthis
已经绑定到其他内容,那么你所能做的就不是很多了。 - Seiyria