napi_run_script
NAPI_EXTERN napi_status napi_run_script(napi_env env,
napi_value script,
napi_value* result);
-
[in] env
:调用 API 的环境。¥
[in] env
: The environment that the API is invoked under. -
[in] script
:包含要执行的脚本的 JavaScript 字符串。¥
[in] script
: A JavaScript string containing the script to execute. -
[out] result
:执行脚本产生的值。¥
[out] result
: The value resulting from having executed the script.
此函数执行一串 JavaScript 代码并返回其结果,但有以下注意事项:
¥This function executes a string of JavaScript code and returns its result with the following caveats:
-
与
eval
不同,此函数不允许脚本访问当前词法范围,因此也不允许访问 模块作用域,这意味着像require
这样的伪全局变量将不可用。¥Unlike
eval
, this function does not allow the script to access the current lexical scope, and therefore also does not allow to access the module scope, meaning that pseudo-globals such asrequire
will not be available. -
该脚本可以访问 全局作用域。脚本中的函数和
var
声明将添加到global
对象中。使用let
和const
进行的变量声明将在全局可见,但不会添加到global
对象中。¥The script can access the global scope. Function and
var
declarations in the script will be added to theglobal
object. Variable declarations made usinglet
andconst
will be visible globally, but will not be added to theglobal
object. -
this
的值是脚本中的global
。¥The value of
this
isglobal
within the script.