Using the following example from the MDN documentation for Arrow functions, at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
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?
edit: perhaps this is the reason? ES6 arrow function lexical this in V8
Difference is, they're discussing Chrome while this question is about Node.js. According to http://kangax.github.io/compat-table/es6/#arrow_functions they have different levels of ES6 support even though they both use V8.