Arrow functions not working in node --harmony under Ubuntu

送分小仙女□ 提交于 2019-11-30 02:46:00

问题


I'm trying to use arrow functions in node v0.10.33 under Ubuntu 14.04 (I'm running node with --harmony flag), but I'm getting this error:

console.log( [1,2,3,4].map(x => x*x) );
                          ^
SyntaxError: Unexpected token >
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

回答1:


This should work now in node v0.12.x, with the --harmony flag. Also note that you can get arrow functions in node using the babel package.

http://babeljs.io/docs/using-babel/

UPDATE:

As correctly indicated by Mike 'Pomax' Kamermans, the --harmony flag is not required when using Node.js >= 4.x. Please see https://nodejs.org/en/docs/es6/ for more information on the status of ES6 support in Node.js.




回答2:


Node, even with the harmony flag, doesn't support the fat arrow yet. Source.

Edit: Fun little fact, ES6 support is one of the reasons Node got forked into io.js. Check out their page on ES6 features - they provide a flag for the arrow functions, but discourage using it.



来源:https://stackoverflow.com/questions/28141657/arrow-functions-not-working-in-node-harmony-under-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!