2

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.

3
  • Sure looks like you've found the reason. That question was from just under a month ago. Commented Feb 20, 2015 at 21:43
  • as @JLRishe noted, if you've found a solution you should post an answer to this question and accept it. Commented Feb 21, 2015 at 0:17
  • Just posted the answer, but I was hoping someone else could confirm before I did that. Commented Feb 21, 2015 at 0:54

1 Answer 1

2

Seems like the reason is that V8 has not updated to support lexical this yet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.