Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js support for => (arrow function)

Is Node.js supporting => function keyword alias already? If yes, starting from which version? How to enable this language extension?

(function() { console.log('it works!') })()

Becomes

(() => { console.log('it works!') })()
like image 936
exebook Avatar asked Oct 28 '13 20:10

exebook


People also ask

Does Nodejs support arrow function?

In short: yes, arrow functions are reasonably well supported in Node. js since version 4.4.

What does => mean in JS?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

How are arrow functions () => {} different than traditional function expressions?

Unlike regular functions, arrow functions do not have their own this . The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in the closest non-arrow parent function.

What is arrow function in node JS?

Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions in a cleaner way compared to regular functions. For example, This function // function expression let x = function(x, y) { return x * y; }


4 Answers

In short: yes, arrow functions are reasonably well supported in Node.js since version 4.4.5.

Completely correct support starts with version 6. Initial support was introduced as far as v0.12 but is was very incomplete and disabled by default until v4.0 when it got better. See Node's ES6 compatibility table for details: http://node.green/#ES2015-functions-arrow-functions.

like image 74
Diego V Avatar answered Oct 18 '22 03:10

Diego V


The syntax you're referring to is "arrow function" syntax. It is a feature of ECMAScript 6, also known as "Harmony". The ES6 standard is now finalized, but engines are still implementing its new features.

The V8 now has arrow function support. Node runs on the V8 engine, but it can take some time for Node to incorporate the latest version into its code base.

Whenever it is added, it might possibly be enabled only via a --harmony command-line flag.

like image 24
apsillers Avatar answered Oct 18 '22 03:10

apsillers


You can follow this issue: https://code.google.com/p/v8/issues/detail?id=2700

Currently (as 02.05.2014) arrow functions have been implemented and waiting until this functionality will be landed in v8: https://codereview.chromium.org/160073006/

After then we'll need to wait, until v8 version with arrow function would be integrated into Node.JS. You can follow Node.JS changelog there: https://github.com/joyent/node/blob/master/ChangeLog (search for "v8: upgrade to ....")

like image 28
Valentyn Shybanov Avatar answered Oct 18 '22 05:10

Valentyn Shybanov


kangax's compatibility tables can keep you up-to-date with what is currently available in Node.

Experimental features can be enabled using the instructions on this page:

All shipping features are turned on by default on Node.js

Staged feature require a runtime flag: --es_staging (or its synonym, --harmony)

In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring) but this is highly discouraged

like image 44
joeytwiddle Avatar answered Oct 18 '22 05:10

joeytwiddle