The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
Object.key 的顺序是根据 for...in 决定的
规范里说到
The mechanics of enumerating the properties is implementation dependent. The order of enumeration is defined by the object.
而且
It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.
根据 MDN 上的解释
Object.key 的顺序是根据
for...in
决定的规范里说到
而且
Object 是个无序的集合
所以应该是各个浏览器实现
for...in
Object 的结果吧这里也有一些讨论
http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop
http://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
这里有
Object.keys
的文档首先
Object.keys
和for in
顺序没任何区别答案很简单只有数字的部分会放到最前面然后排序 其他部分会放到后面 这只是js的处理机制,
根据一般的代码规范你这样使用本身就不合理~有些语言直接报错~
可以参看 这个书的介绍 http://exploringjs.com/es6/ch_oop-besides-classes.html#sec_iterating-property-keys
Object.keys()
和for...in
输出顺序一样输出顺序规则为: