new vm.Script(code[, options])


  • code <string> 编译 JavaScript 代码。
  • options <Object> | <string>
    • filename <string> 指定此脚本生成的堆栈跟踪中使用的文件名。默认值: 'evalmachine.<anonymous>'
    • lineOffset <number> 指定此脚本生成的堆栈跟踪中显示的行号偏移。默认值: 0
    • columnOffset <number> 指定此脚本生成的堆栈跟踪中显示的首行列偏移量。默认值: 0
    • cachedData <Buffer> | <TypedArray> | <DataView> 提供一个可选的 BufferTypedArrayDataView,用于包含所提供源代码的 V8 代码缓存数据。提供时,cachedDataRejected 的值将根据 V8 对数据的接受情况被设置为 truefalse
    • produceCachedData <boolean> 当设置为 true 且没有 cachedData 时,V8 会尝试为 code 生成代码缓存数据。成功后,将生成一个包含 V8 代码缓存数据的 Buffer,并存储在返回的 vm.Script 实例的 cachedData 属性中。cachedDataProduced 的值将根据是否成功生成代码缓存数据而设为 truefalse。该选项已不推荐使用,建议使用 script.createCachedData() 代替。默认值: false.
    • importModuleDynamically <Function> | <vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER> 用于指定在调用 import() 时评估此脚本时模块应如何加载。此选项是实验性模块 API 的一部分。我们不建议在生产环境中使用它。有关详细信息,请参见 在编译 API 中支持动态 import()

如果 options 是字符串,那么它指定文件名。

【If options is a string, then it specifies the filename.】

创建一个新的 vm.Script 对象会编译 code,但不会执行它。编译后的 vm.Script 可以在之后多次运行。code 不会绑定到任何全局对象;相反,它会在每次运行之前绑定,仅用于那次运行。

【Creating a new vm.Script object compiles code but does not run it. The compiled vm.Script can be run later multiple times. The code is not bound to any global object; rather, it is bound before each run, just for that run.】