由于JavaScript不保证对象中的属性顺序,因此JSON.stringify()实际上是如何工作的?

由于JavaScript不保证对象中的属性顺序,因此JSON.stringify()实际上是如何工作的?,javascript,json,object,serialization,idempotent,Javascript,Json,Object,Serialization,Idempotent,既然如此,你到底是怎么表现的 以下是否始终为真(同一对象) const o={a:1,b:2}; log(JSON.stringify(o)==JSON.stringify(o))我查看了JSON.stringify的ECMAScript标准: 这似乎是有益的: 对于K的每个元素p Let strP be the result of calling the abstract operation Str with arguments P and value. If strP is not und

既然如此,你到底是怎么表现的

  • 以下是否始终为真(同一对象)
  • const o={a:1,b:2};
    
    log(JSON.stringify(o)==JSON.stringify(o))我查看了
    JSON.stringify的ECMAScript标准:

    这似乎是有益的:

    对于K的每个元素p

    Let strP be the result of calling the abstract operation Str with arguments P and value.
    If strP is not undefined
        Let member be the result of calling the abstract operation Quote with argument P.
        Let member be the concatenation of member and the colon character.
        If gap is not the empty String
            Let member be the concatenation of member and the space character.
        Let member be the concatenation of member and strP.
        Append member to partial.
    
    最后一步中的“append”强烈地暗示了结果是按源代码排序的,我可以确认您的代码断言同时传递到Chromium和Firefox上

    编辑:对于“K中的p”,这可能也相关:

    字符串的顺序应与Object.keys标准内置函数使用的顺序相同


    只要将比较保存在一个浏览器中,您的断言似乎是正确的。

    No.Append意味着字符串是按照“K中的每个元素p”的顺序构建的,这并不意味着它们是按源排序的(或者动态生成的对象的任何顺序)。1)是,同一对象上的枚举顺序始终保证相同。2) 应该是的,尽管没有严格的保证我们会期待决定论。3) 您不能“使”这一点为真,您应该使用而不是比较
    JSON.stringify
    结果