Javascript node.js 是否支持“let”语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11283538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does node.js support the 'let' statement?
提问by ben author
Does node.js support a let statement something like what's described on MDN??
node.js 是否支持类似于MDN上描述的 let 语句??
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
和/或是否有另一个 js 环境
- has
let
and and - has a REPL, as node does? Rhino?
- 有
let
和 - 有 REPL,就像节点一样?犀牛?
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.
这个问题很久以前就被问到了。截至 2015 年底,答案是“是的,是的”。Harmony 特性默认包含在 io.js 3.3 中,并且最近在 4.x 版本中被带回 node.js。
采纳答案by Todd Yandell
I don't think Node supports let
, but you can do this:
我不认为 Node 支持let
,但你可以这样做:
var a = 5;
(function () {
var a = 6;
console.log(a); // => 6
})();
console.log(a); // => 5
回答by Timothy Strimple
Yes, you can use let within node.js, however you have to run node using the optional --harmony flag. Try the following test.js:
是的,您可以在 node.js 中使用 let,但是您必须使用可选的 --harmony 标志运行 node。尝试以下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:
然后运行该文件node --harmony test.js
,结果为:
15
20
I would not recommend using this in an important production application, but the functionality is available now.
我不建议在重要的生产应用程序中使用它,但该功能现在可用。
回答by JohnnyHK
回答by Shamaoke
node --use_strict --harmony_scoping
回答by Johnny Oshika
You can use the Babel transpiler and use let
as well as many other ES6/ES2015 features.
您可以使用 Babel 转译器并使用let
许多其他 ES6/ES2015 特性。
To use babel:
使用巴贝尔:
$ npm install --save-dev babel
Then in your package.json
:
然后在您的package.json
:
"scripts": {
"start": "babel-node index.js"
}
Inside index.js
:
内部index.js
:
let foo = 'bar;
Then start the server:
然后启动服务器:
$ npm start