vendredi 20 février 2015

Arrow function context in node.js


Using the following example from the MDN documentation for Arrow functions, at http://ift.tt/1qcr1te



function Person(){
this.age = 0;

setInterval(() => {
this.age++; // |this| properly refers to the person object
}, 1000);
}

var p = new Person();


When I copy/paste that code into node.js 0.12 node --harmony, the this.age++; line doesn't seem to be referring to Person context, but rather the setInterval context. Adding console.log(this) to the callback seems to confirm that.


When I've used other es6->es5 transpilers, it has always worked as expected. Is this a bug in node.js? Am I missing something?





Aucun commentaire:

Enregistrer un commentaire