dat*_*uoc 1 javascript eval node.js ecmascript-6 mjs
似乎不可能eval在 Node.js ES6 中使用创建变量,但我不明白为什么。这在 CentOS 7 上发生在我身上,但我不认为操作系统是这里的问题。
常规 Node.js 文件(test.js):
eval("var a=1");
console.log(a);
Run Code Online (Sandbox Code Playgroud)
使用 .mjs 扩展名制作相同的文件以与 Node.js ES6 (test.mjs) 一起运行:
eval("var a=1");
console.log(a);
Run Code Online (Sandbox Code Playgroud)
之后,使用 Node.js 和 Node.js ES6 运行 2 个文件:
$ node test.js
1
$ node --experimental-modules test.mjs
(node:9966) ExperimentalWarning: The ESM module loader is experimental.
ReferenceError: a is not defined
at file:///temp/test.mjs:2:13
at ModuleJob.run (internal/modules/esm/module_job.js:96:12)
Run Code Online (Sandbox Code Playgroud)
它是与 ES6 相关的问题吗?我在浏览器的控制台上试过,问题是一样的:
>> eval("var a=1"); console.log(a);
1
>> class c { static f(){ eval("var a=1"); console.log(a); } }
c.f()
ReferenceError: a is not defined
Run Code Online (Sandbox Code Playgroud)
我正在使用 Node.js 10.9.0,这是一个错误还是背后有原因?