简体   繁体   English

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

[英]How to comprehend clause 10.5 in ECMA-262 5.1 Edition?

Recently I read about the ES5 Specification, there's one confusion in Chapter-10 which is about Execution Context . 最近,我读到有关ES5规范的信息,在第10章中有一个关于执行上下文的困惑。 More exactly, the confusion exists in 10.5[ https://ecma-international.org/ecma-262/5.1/#sec-10.5 ]. 更确切地说,混淆存在于10.5 [ https://ecma-international.org/ecma-262/5.1/#sec-10.5 ]中。

The clause 10.5 named Declaration Binding Instantiation , it explains how the component VariableEnvironment of Execution Context is generated.Where Im confused is the item-5-iii : " If existingProp .[[Configurable]] is true... ". 第10.5条名为“声明绑定实例化” ,它说明了如何生成执行上下文的 VariableEnvironment组件。 在项目5 iii中, Im感到困惑的是:“如果existProp。[[Configurable]]是true ...”。

What's the purpose for this, why the PropertyDescriptor.[[Value]] is undefined when call [[DefineOwnProperty]] of global object , and how to prove this step with real javascript code? 这样做的目的是什么, 为什么在调用全局对象的[[DefineOwnProperty]]时未定义PropertyDescriptor。[[Value]] ,以及如何使用真实的javascript代码来证明这一步骤?

Or maybe this is a mistake? 也许这是一个错误? Here the [[Value]] should be the declared function object? 这里的[[Value]]应该是声明的函数对象?

When a function is declared on the top level, it checks to see if the property name exists on the global object first. 当在顶层声明一个函数时,它首先检查该属性名是否存在于全局对象上。 If the property does not exist, then: 如果该属性不存在,则:

c. C。 Let funcAlreadyDeclared be the result of calling env's HasBinding concrete method passing fn as the argument. 令funcAlreadyDeclared为通过fn作为参数调用env的HasBinding具体方法的结果。

d. d。 If funcAlreadyDeclared is false, call env's CreateMutableBinding concrete method passing fn and configurableBindings as the arguments. 如果funcAlreadyDeclared为false,则调用fn和configurableBindings作为参数,调用env的CreateMutableBinding具体方法。

Otherwise, it goes into the e. 否则,它将进入e. part that you're looking at: 您正在查看的部分:

e. Else if env is the environment record component of the global environment then: ... 否则,如果env是全局环境的环境记录组件,则:...

So, anywhere inside that e. 因此,在该e.任何位置e. , funcAlreadyDeclared will necessarily be true - the property is already defined, and what remains is to check to see if the property is changeable. funcAlreadyDeclared必然是true -属性已经定义,剩下的就是要检查是否该属性是可变的。 The PropertyDescriptor.[[Value]] will necessarily return a full property descriptor, because inside e. PropertyDescriptor.[[Value]] 将必须返回完整的属性描述符,因为在e. , we know that the property does exist; ,我们知道该属性确实存在; that block only runs if funcAlreadyDeclared is true . 该块仅在funcAlreadyDeclaredtrue运行。

On the top level, it checks if the property is configurable, and if so, sets the associated property on the global object. 在顶层,它检查属性是否可配置,如果是,则在全局对象上设置关联的属性。 Eg, function foo(){} on the top level will result in window.foo being defined, and this section checks that window.foo can be defined. 例如,顶层function foo(){}将导致定义window.foo ,并且本节检查是否可以定义window.foo

Having configurable of true means : 具有true configurable意味着

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. 当且仅当此属性描述符的类型可以更改并且该属性可以从相应对象中删除时,才返回true。

For example, window.top is not configurable, so the [[DefineOwnProperty]] will not run: 例如, window.top是不可配置的,因此[[DefineOwnProperty]]将不会运行:

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

So, trying to declare a function named top on the top level will throw an error: 因此,尝试在top声明一个名为top的函数将引发错误:

 function top() { } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM