Arrow function context in node.js

az_

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.

az_

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Node.js support for => (arrow function)

From Dev

This context in arrow function ReactJS

From Dev

arrow function inside parentheses {} about react context

From Dev

arrow function inside parentheses {} about react context

From Dev

difference between function context (this) in node.js and browser

From Dev

Migrating to Arrow function from Normal function JS

From Dev

Global context and "this" in Node.js

From Dev

Es6 Arrow function to normal js

From Dev

Context visiability for function in js (React.js)

From Dev

Node.js - Is not a function

From Dev

Export a function with Node JS

From Dev

.forEach is not a function node JS

From Dev

node js callback function

From Dev

Node.js - Is not a function

From Dev

Node js, function execution

From Dev

Context IO node.js client library

From Dev

Node.js error : failed to initialize context

From Dev

Set jQuery function to a context of "this" - knobknob.js

From Dev

Simple JS Arrow Function: Missing Parenthesis After Argument List: Why?

From Dev

Why Chrome can not(or CAN) find context in a Arrow function of ES6

From Dev

Scope and/or context in Node.js / Sails.js application

From Dev

What is the meaning of tilde arrow in this context?

From Dev

Arrow function is not a function

From Dev

TypeError: object is not a function Node js Node Authentication

From Dev

Call a function in node.js

From Dev

Node.JS readFileSync() function

From Dev

What are these node js variables at the of the function?

From Dev

Node.js Function Flow

From Dev

TypeError: Object is not a function Node JS