gpt4 book ai didi

javascript - 如何理解ECMA-262 5.1版中的条款10.5?

转载 作者:行者123 更新时间:2023-11-30 13:56:50 25 4
gpt4 key购买 nike

最近我读到有关 ES5 规范的内容,第 10 章 中有一个关于执行上下文 的混淆。更确切地说,混淆存在于 10.5[ https://ecma-international.org/ecma-262/5.1/#sec-10.5 ].

第10.5条Declaration Binding Instantiation,它解释了Execution Context的组件VariableEnvironment是如何生成的。我困惑的地方是item-5-iii:“如果 existingProp .[[Configurable]] 为真...”。

这样做的目的是什么,为什么在调用全局对象的 [[DefineOwnProperty]] 时 PropertyDescriptor.[[Value]] 未定义,如何用真实的 javascript 代码证明这一步?

或者这是一个错误?这里的[[Value]]应该是声明的函数对象吧?

最佳答案

当在顶层声明一个函数时,它会首先检查属性名称是否存在于全局对象上。如果该属性不存在,则:

c. Let funcAlreadyDeclared be the result of calling env’s HasBinding concrete method passing fn as the argument.

d. If funcAlreadyDeclared is false, call env’s CreateMutableBinding concrete method passing fn and configurableBindings as the arguments.

否则,它会进入您正在查看的 e. 部分:

e. Else if env is the environment record component of the global environment then: ...

因此,e. 中的任何地方,funcAlreadyDeclared 都必须是 true - 属性 已经定义,剩下的就是检查属性是否可变。 PropertyDescriptor.[[Value]] 必然会返回一个完整的属性描述符,因为在 e. 中,我们知道该属性确实存在;该 block 仅在 funcAlreadyDeclaredtrue 时运行。

在顶层,它检查属性是否可配置,如果是,则在全局对象上设置关联的属性。例如,顶层的 function foo(){} 将导致 window.foo 被定义,这部分检查 window.foo可以定义。

具有 可配置true means :

true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.

例如,window.top 是不可配置的,所以 [[DefineOwnProperty]] 将不会运行:

console.log(Object.getOwnPropertyDescriptor(window, 'top'));

因此,尝试在顶层声明一个名为 top 的函数会抛出错误:

function top() {
}

关于javascript - 如何理解ECMA-262 5.1版中的条款10.5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57107527/

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