【问题标题】:--harmony flag in Node.js 0.12 throws SyntaxErrorNode.js 0.12 中的 --harmony 标志抛出 SyntaxError
【发布时间】:2015-03-30 21:54:21
【问题描述】:

所以我写了以下内容:

index.js

let x = 31

console.log(x)

并尝试使用--harmony 标志对其进行测试:

alex@alex-K43U:~/node/es6$ node --harmony index.js

但我得到一个错误:

/home/alex/node/es6/index.js:1
(function (exports, require, module, __filename, __dirname) { let x = 31
                                                            ^
SyntaxError: Illegal let declaration outside extended mode

我做错了什么?我已经更新到节点0.12.1

【问题讨论】:

    标签: javascript node.js ecmascript-6


    【解决方案1】:

    您需要启用严格模式才能使用let

    您可以:

    "use strict"
    let x = 31
    console.log(x)
    

    或者:

    node --harmony --use_strict index.js
    

    然后它按预期工作:

    victor@ubuntu:~/Documents$ node --harmony --use_strict index.js
    31
    

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2015-02-27
      相关资源
      最近更新 更多