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> 提供可选的 BufferTypedArray,或者 DataView,用于提供的源的 V8 代码缓存数据。提供后,cachedDataRejected 的值将根据 V8 是否接受数据而设置为 truefalse
    • produceCachedData <boolean> 当存在 true 而不存在 cachedData 时,V8 将尝试为 code 生成代码缓存数据。成功后,将生成一个包含 V8 代码缓存数据的 Buffer,并存储在返回的 vm.Script 实例的 cachedData 属性中。cachedDataProduced 的值将根据代码缓存数据是否生成成功而设置为 truefalse。该选项已 弃用,建议使用 script.createCachedData()默认值: false
    • importModuleDynamically <Function> 在评估此模块时调用,当调用 import() 时触发。如果未指定此选项,则对 import() 的调用将被 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING 拒绝。此选项属于实验性模块 API。我们不推荐在生产环境中使用它。如果未设置 --experimental-vm-modules,此回调将被忽略,对 import() 的调用将被 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG 拒绝。
      • specifier <string> 传递给 import() 的说明符
      • script <vm.Script>
      • importAttributes <Object> 传递给 [optionsExpression][optionsExpression] 可选参数的 "with" 值,如果未提供值,则为一个空对象。
      • 返回:<Module Namespace Object> | <vm.Module> 建议返回 vm.Module,以便利用错误跟踪,并避免包含 then 函数导出的命名空间出现问题。

如果 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.