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> 提供一个可选的Buffer、TypedArray或DataView,用于包含所提供源代码的 V8 代码缓存数据。提供时,cachedDataRejected的值将根据 V8 对数据的接受情况被设置为true或false。produceCachedData<boolean> 当设置为true且没有cachedData时,V8 会尝试为code生成代码缓存数据。成功后,将生成一个包含 V8 代码缓存数据的Buffer,并存储在返回的vm.Script实例的cachedData属性中。cachedDataProduced的值将根据是否成功生成代码缓存数据而设为true或false。该选项已不推荐使用,建议使用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可选参数的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.】