Are Javascript Object Properties assigned in order?

前端 未结 5 893
深忆病人
深忆病人 2020-12-03 14:11

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 = {
                  


        
5条回答
  •  盖世英雄少女心
    2020-12-03 14:21

    Standard ECMA-262 (5.1) - Section 11.1.5 - Object Initialiser

    The production PropertyNameAndValueList : PropertyNameAndValueList , PropertyAssignment is evaluated as follows:

    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.

提交回复
热议问题