在节点0.11中使用ES6箭头函数w / Foo.prototype

我得到了我在使用原型扩展中使用箭头函数的意外行为。

function ES6Example(){} ES6Example.prototype.foo = function(bar){ return ((baz) => { console.log(this) this.bar = baz })(bar) } var es6Example = new ES6Example es6Example.foo('qux') console.info(es6Example.bar) 

上面的代码导致全局上下文被打印出来, es6Example.bar也是未定义的。 这是旧的行为。 基于我在MDN中看到的文档,我期望这个绑定到实例。 我正在使用和谐标志使用节点v0.11.15运行上面的代码。 请注意,下面的工作:

 function ES6Example(){ this.foo = baz => { this.bar = baz } } 

V8的实现还不完整,这里还是没有词法。

这就是为什么在Chrome node.js和io.js中,你必须设置一个特殊的“和谐”参数才能使用它:它并没有准备好用于一般的使用。