vm.compileFunction(code[, params[, options]])


  • code <string> 要编译的函数体。
  • params <string[]> 包含函数所有参数的字符串数组。
  • options <Object>
    • filename <string> 指定此脚本生成的堆栈跟踪中使用的文件名。默认值: ''
    • lineOffset <number> 指定此脚本生成的堆栈跟踪中显示的行号偏移。默认值:0
    • columnOffset <number> 指定此脚本生成的堆栈跟踪中显示的第一行列号偏移。默认值:0
    • cachedData <Buffer> | <TypedArray> | <DataView> 提供可选的 BufferTypedArray,或者 DataView,用于所提供源的 V8 代码缓存数据。
    • produceCachedData <boolean> 指定是否生成新的缓存数据。 默认值: false
    • parsingContext <Object> 应在其中编译上述函数的 情境化 对象。
    • contextExtensions <Object[]> 一个包含上下文扩展集合的数组(封装当前作用域的对象),将在编译时应用。默认值: []
    • importModuleDynamically <Function> 在评估此模块时调用,当调用 import() 时执行。如果未指定此选项,对 import() 的调用将会因 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING 而被拒绝。此选项是实验性模块 API 的一部分,不应被视为稳定。
      • specifier <string> 传递给 import() 的说明符
      • function <Function>
      • importAssertions <Object> 传递给 [optionsExpression][optionsExpression] 可选参数的 "assert" 值,如果未提供值,则为一个空对象。
      • 返回:<Module Namespace Object> | <vm.Module> 建议返回 vm.Module,以便利用错误跟踪,并避免包含 then 函数导出的命名空间出现问题。
  • 返回:<Function>

将给定的代码编译到提供的上下文中(如果未提供上下文,则使用当前上下文),并将其封装在包含指定 params 的函数中返回。

🌐 Compiles the given code into the provided context (if no context is supplied, the current context is used), and returns it wrapped inside a function with the given params.