The object itself is a set of objects without objects. Therefore, when you use the for-in statement to traverse the attributes of an object, the order of the retrieved attributes is different from that of the object definition. Understanding W3C standards: As described in the third edition of ECMA-262 (ECMAScript), f
The object itself is a set of objects without objects. Therefore, when you use the for-in statement to traverse the attributes of an object, the order of the retrieved attributes is different from that of the object definition.
Understand W3C standards:
As described in the third edition of ECMA-262 (ECMAScript), the order of attribute traversal for a for-in statement isThe order in which attributes are written depends on the object definition..
For more information about The for-in Statement in The third Edition of ECMA-262, see 12.6.4 The for-in Statement in ECMA-262 3rd Edition.
In the latest ECMA-262 (ECMAScript) Fifth Edition specification, the for-in statement traversal mechanism has been adjusted,The order of attribute traversal is not specified..
For more information about The for-in Statement in ECMA-262 (ECMAScript) Fifth Edition, see 12.6.4 The for-in Statement in ECMA-262 5rd Edition.
The attribute traversal order in the new version is different from that in earlier versions, which will lead to JavaScript parsing engines implemented following the ECMA-262 third-edition specification when processing for-in statements, the traversal sequence of attributes is inconsistent with the parsing engine implemented in compliance with the fifth edition specification.
Therefore, we should try to avoid writing code that depends on the object attribute sequence during development. As follows:
Script var json1 = {"2": {"name": "1st"}, "1": {"name": "2nd "}, "3": {"name": "3rd"} var json2 = [{"name": "1st" },{ "name ": "2nd" },{ "name": "3rd"}] for (var I in json1) {alert (json1 [I]. name);} // correct for (var I in json2) {alert (json2 [I]. name);} script
Let's take a look at the differences between the for-in code in different browsers: