简体   繁体   English

是否按顺序分配了Javascript对象属性?

[英]Are Javascript Object Properties assigned in order?

Say I have an object which assigns properties based off the return value of a function: 说我有一个对象,该对象根据函数的返回值分配属性:

var i = 0;

var f = function() { return ++i; }

var foo = {
            a:f(),
            b:f(),
            c:f()
          };

Is it guaranteed that foo.a will be 1, foo.b will be 2, and foo.c will be 3? 是否可以保证foo.a为1,foo.b为2,foo.c为3? I know that JS doesn't guarantee order when you iterate over an object, what about assignment? 我知道当您迭代对象时JS不能保证顺序,那分配呢?

Is it specified in the JS specification somewhere? 是在JS规范中指定的吗? I'm only asking for educational reasons. 我只是出于教育原因。

Thanks. 谢谢。

Standard ECMA-262 (5.1) - Section 11.1.5 - Object Initialiser 标准ECMA-262(5.1)-第11.1.5节-对象初始化程序

The production PropertyNameAndValueList : PropertyNameAndValueList , PropertyAssignment is evaluated as follows: 生产PropertyNameAndValueList:PropertyNameAndValueList,PropertyAssignment的评估如下:

 1. Let obj be the result of evaluating PropertyNameAndValueList. 2. Let propId be the result of evaluating PropertyAssignment. ... 5. Call the [[DefineOwnProperty]] internal method of obj with arguments propId.name, propId.descriptor, and false. 6. Return obj. 

So yes, the order is enforced by the standard. 因此,是的,订单由标准强制执行。

From the ECMAScript 6 wiki, which will define the new version of JS: 来自ECMAScript 6 Wiki,它将定义JS的新版本:

When a scope (Block, FunctionBody, Program, ModuleBody, etc.) is entered, the variables declared by all immediately contained function and class declarations are bound to their respective functions and classes. 当输入范围(Block,FunctionBody,Program,ModuleBody等)时,所有立即包含的函数和类声明所声明的变量将绑定到其各自的函数和类。 Then all class bodies are executed in textual order . 然后,所有类主体均按文本顺序执行 A class body defines and initializes class-wide properties once when the class definition is evaluated. 当评估类定义时,类主体会定义并初始化类范围的属性。 This includes properties on the constructor function (the “class” itself) and on its prototype property. 这包括构造函数的属性(“类”本身)及其原型属性。 These initializations happen in textual order . 这些初始化以文本顺序发生

Your source has arrived! 您的消息来源已经到来! JavaScript object properties are initialized in textual order on objects . JavaScript对象属性按对象上的文本顺序初始化。 Arrays do not (currently) always follow this rule. 数组(当前)并不总是遵循此规则。

Source : http://wiki.ecmascript.org/doku.php?id=harmony:classes 来源http : //wiki.ecmascript.org/doku.php?id= harmony : classes

I will edit this post when I find the reference in ECMAScript 5, though I am certain it is there. 在ECMAScript 5中找到引用后,我将对其进行编辑,尽管我确信它在那里。

Edit: Found it 编辑:找到它

ECMAScript 5 does have it: http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.7 . ECMAScript 5确实具有它: http : //www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.7

If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used to order the list elements in step 3 of this algorithm. 如果实现为for-in语句定义了特定的枚举顺序,则在该算法的步骤3中,必须使用相同的枚举顺序对列表元素进行排序。

This defines the calls to DefineOwnProperty and therefore the position of the properties in the internal table. 这定义了对DefineOwnProperty的调用,并因此定义了属性在内部表中的位置。

Assignment is always in order. 分配总是井井有条。 It is just the way the code is found, interpreted and executed. 这就是找到,解释和执行代码的方式。

是的,我们保证a将为1,b将为2,依此f() 。每个f()都将按照您编写它们的顺序进行解释。

I don't have an official source to quote, but let's just use a little common sense. 我没有官方消息来源,但让我们使用一些常识。

What if assignment wasn't done in order? 如果未按顺序进行分配怎么办? You could never know which value would be assigned to which property, making the object structure useless (at least when using the object literal syntax) . 您永远无法知道将哪个值分配给哪个属性,从而使对象结构无用(至少在使用对象文字语法时)

It makes no difference if it's a function call, or a primitive literal, or some other value. 如果是函数调用,原始文字或其他值,则没有区别。 If it wasn't guaranteed to happen in order, it just wouldn't work. 如果不能保证顺序发生,那就行不通了。


After doing a quick search of the term left to right in ECMAScript 5, here are some results if it helps you: 在ECMAScript 5中快速搜索left to right的术语后,以下一些结果对您有帮助:

7 Lexical Conventions 7个词汇惯例

The source text of an ECMAScript program is first converted into a sequence of input elements, which are tokens, line terminators, comments, or white space. 首先将ECMAScript程序的源文本转换为一系列输入元素,这些输入元素是标记,行终止符,注释或空白。 The source text is scanned from left to right, repeatedly taking the longest possible sequence of characters as the next input element. 从左到右扫描源文本,重复以尽可能长的字符序列作为下一个输入元素。

11.8.5 The Abstract Relational Comparison Algorithm 11.8.5抽象关系比较算法

...It is necessary because ECMAScript specifies left to right evaluation of expressions. ...这是必需的,因为ECMAScript指定了从左到右的表达式求值。

Annex D (informative) Corrections and Clarifications in the 5th Edition with Possible 3rd Edition Compatibility Impact 第5版中的附录D(信息性)更正和澄清,可能会对第3版产生兼容性影响

ECMAScript generally uses a left to right evaluation order, however the Edition 3 specification language for the > and <= operators resulted in a partial right to left order. ECMAScript通常使用从左到右的评估顺序,但是用于>和<=运算符的Edition 3规范语言导致部分从右到左的顺序。 The specification has been corrected for these operators such that it now specifies a full left to right evaluation order. 已针对这些运算符对规范进行了更正,以使其现在指定了从左到右的完整评估顺序。

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

相关问题 是否按照相同的顺序分配了对象属性? - Are object properties assigned in the same order they are declared? JavaScript:指定对象中属性的顺序 - JavaScript: Specifying the Order of Properties In An Object 如何在javascript中获取分配给函数对象的所有属性? - How to get all the properties assigned to the function object in javascript? 基于定义值的Javascript订单对象属性 - Javascript order object properties based on defined value JavaScript 对象未正确分配 - JavaScript Object is not assigned properly Javascript 中的变量分配顺序是什么? - In which order are variables assigned in Javascript? Javascript Intellisense 不再适用于在 Visual Studio 2022 中创建初始对象后分配的对象属性 - Javascript Intellisense no longer working for object properties assigned after initial object creation in Visual Studio 2022 Javascript-按对象属性的指定顺序对对象数组进行排序 - Javascript— sort an array of objects with a specified order of object properties 如何按照写入顺序迭代 javascript object 属性 - How to iterate javascript object properties in the order they were written 基于带索引的数组 (JavaScript) 更改 object 属性的顺序 - Change order of object properties based on array with indices (JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM