javascript - 为什么我无法从命令行通过 "node --harmony test.js"启动和谐模式?

标签 javascript node.js ecmascript-6 ecmascript-harmony

问题是:

longhao33@hePC:~$ node --harmony test.js 
/home/longhao33/test.js:1
(function (exports, require, module, __filename, __dirname) { let str = 'es666666666666';
                                                              ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

但是在以下情况下支持“let”:(太奇怪了!)

longhao33@hePC:~$ node --harmony
> let str = 'es66666666666'
undefined
> str
'es66666666666'
  1. 系统:ubuntu 14.04LTS 64位
  2. node:V4.1.1(由nvm安装,安装在$HOME)
  3. test.js 的内容:

    let str = 'es666666666666'; console.log(str);

提前致谢。

最佳答案

您在 test.js 顶部缺少 'use strict';(如错误消息所示)。

它可以在 REPL 中运行,因为 'use strict'; is automatically included during code evaluation .

关于javascript - 为什么我无法从命令行通过 "node --harmony test.js"启动和谐模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32891755/

相关文章:

javascript - 不公开扩展类方法

javascript - 将两个输入变量相乘jscript

node.js - npm 错误!缺少脚本 : deploy

node.js - 如何组织多个 Redis 客户端

javascript - 在 Javascript 数组中查找重复元素

javascript - 无法在普通 JS 中导出类

javascript - 如何知道哪些 JavaScript 函数绑定(bind)到 HTML 元素?

javascript - ng-disabled 不适用于 span 标签

javascript - 如果原型(prototype)无法访问私有(private)变量,那么 "clean up"代码的最佳方法是什么?

android - 文件 style.xml 中不支持的 Node 'item'