Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - Does node.js support the 'let' statement?

Does node.js support a let statement something like what's described on MDN??

var x = 8,
    y = 12;

let ( x = 5, y = 10) {
    return x + y;
} //15

If not, is there a way to duplicate the functionality with a self-executing anonymous function or something?

And/or is there another js environment that

  1. has let and and
  2. has a REPL, as node does? Rhino?

EDIT:

This question was asked quite a while ago. As of now, late 2015, the answer is "Yes, yes it does". Harmony features were included by default in io.js 3.3, and have been recently brought back to node.js with the 4.x release.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you can use let within node.js, however you have to run node using the optional --harmony flag. Try the following test.js:

"use strict"
var x = 8,
    y = 12;

{ let x = 5, y = 10; console.log(x + y); }

console.log(x + y);

And then run the file node --harmony test.js which results in:

15
20

I would not recommend using this in an important production application, but the functionality is available now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...