Property Iteration in JSON serialization

Brian Kardell bkardell at gmail.com
Wed Oct 14 23:18:23 PDT 2009


For even more fun try out the code below which has mixed content and
still appears to function (surprisingly) the same in FF 3.5/Safari4
(OSX) and IE8 (XP) -- but seems to order the keys differently in
Chrome 3 - though seemingly still as explained earlier (except that
the negative index is last??):

function Construct(){
    this["-1"] = "holy cow";  //This surprises me"
    this.a = "a";
    this[1] = "what";
    this.b = "b";
    this["1"] = "the";
    this["2"] = "heck";
    this.c = "c";
}

var constrObj = new Construct();
constrObj["3"] = "I don't even know what this is supposed to be!";

function printProperties(description, o) {
    var str = "";
    str += description;
    for (var pname in o) {
        str += " " + pname + " - " + o[pname] + "\n";
    }
    alert(str);
    alert(o[1]);
    alert(o[-1]);
}

printProperties("constructed object:\n", constrObj);




On Wed, Oct 14, 2009 at 10:53 PM, Maciej Stachowiak <mjs at apple.com> wrote:
>
> On Oct 14, 2009, at 10:47 PM, Maciej Stachowiak wrote:
>
>>
>> (*) - If you use constructor functions to make an object with properties
>> named (x, y, z) added in that order, with a prototype that has properties
>> (a, b, c), and in turn has a prototype with properties (q, r, s), JSC and
>> SpiderMonkey report "x y z a b c q r s" but V8 gives "x z y a c b q r s".
>> I'm not sure what rule would give that output. I'm not even sure if this
>> behavior is intentional or a bug.
>
> Correction: on further testing, it appears that Chrome's ordering anomaly is
> for own properties added by a constructor function, whether or not there's a
> prototype with enumerable properties. Chrome reports "a c b" for the
> following, whereas other browser which reports "a b c".
>
> <pre>
> <script>
> function Construct()
> {
>    this.a = "a";
>    this.b = "b";
>    this.c = "c";
> }
>
> var constrObj = new Construct();
>
> function printProperties(description, o) {
>    document.write(description + ":");
>    for (var pname in o) {
>        document.write(" " + pname);
>    }
>    document.write("\n");
> }
>
> printProperties("constructed object", constrObj);
> </script>
> </pre>
>


More information about the es-discuss mailing list