gpt4 book ai didi

javascript - 什么是 "calling context?"

转载 作者:搜寻专家 更新时间:2023-11-01 04:25:29 25 4
gpt4 key购买 nike

ECMA-262 5.1 小节 10.4.210.4.2.1引用“调用上下文”。这似乎没有在文档的其他任何地方描述。

引用规范,强调我的:

10.4.2 Entering Eval Code

The following steps are performed when control enters the execution context for eval code:

  1. If there is no calling context or if the eval code is not being evaluated by a direct call (15.1.2.1.1) to the eval function then,

10.4.2.1 Strict Mode Restrictions

The eval code cannot instantiate variable or function bindings in the variable environment of the calling context that invoked the eval if either the code of the calling context or the eval code is strict code. Instead such bindings are instantiated in a new VariableEnvironment that is only accessible to the eval code.

  • 这些段落中的“调用上下文”是什么意思?我假设它指的是 Execution Context在调用 eval 之前的堆栈顶部;谁能证实这一点?

  • “没有调用上下文”是什么意思?有人可以提供可能导致在没有调用上下文的情况下调用 eval 的代码或条件示例吗?

最佳答案

“调用上下文”是指调用 native eval 函数的上下文。

如果您从一些 native 代码执行 eval(例如,您运行一个 native 函数,该函数在出于某种原因使用 eval 完成时执行代码),那么它没有上下文,然后指定在全局范围内运行。上下文仅指 ECMAScript 可执行代码。

然而,调用上下文是指调用它的执行上下文中的变量和指令。例如,如果它检查调用上下文,它只知道 eval 意味着作为严格代码工作。

ES-Discuss 邮件列表中的一个非常相似的问题对此进行了澄清,其中 Brendan Eich(JavaScript 的创建者)回应:

How can eval code not have a calling context?

ES3 says:

10.2.2 Eval Code

When control enters an execution context for eval code, the previous active execution context, referred to as the calling context, is used to determine the scope chain, the variable object, and the this value. If there is no calling context, then initialising the scope chain, variable instantiation, and determination of the this value are performed just as for global code.

我对“如果没有调用上下文”感到困惑。怎么可能 可能性出现?如果没有人调用它,eval 将如何被调用?

Response:

A call from native code, the "host" program.

Some browsers support indirect eval, allowing this:

setTimeout(eval, 0, "alert('hi mom')");

The window used is the one in which setTimeout was found along the scope chain, so

myFrame.setTimeout(eval, 0, "alert(x)");

should show myFrame.x, not the calling frame or window's x.

注意:此实例中的 myFrame 指的是框架的全局范围。

关于javascript - 什么是 "calling context?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com