似乎不可能eval
在 Node.js ES6 中使用创建变量,但我不明白为什么。这在 CentOS 7 上发生在我身上,但我不认为操作系统是这里的问题。
常规 Node.js 文件(test.js):
eval("var a=1");
console.log(a);
使用 .mjs 扩展名制作相同的文件以与 Node.js ES6 (test.mjs) 一起运行:
eval("var a=1");
console.log(a);
之后,使用 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)
它是与 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
我正在使用 Node.js 10.9.0,这是一个错误还是背后有原因?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句