for in 循环的输出顺序问题
var data = {
'4': 'first',
'3': 'second',
'2': 'third',
'1': 'fourth'
};
for (var i in data) {
console.log(i + " " + data[i])
}
IE11, chrome31, firefox23的打印如下:
1 fourth
2 third
3 second
4 first
var obj = {
"first":"first",
"zoo":"zoo",
"2":"2",
"34":"34",
"1":"1",
"second":"second"
};
for (var i in obj) { console.log(i); };
IE11, chrome31, firefox23的打印如下:
1
2
34
first
zoo
second
Currently all major browsers loop over the properties of an object in the order in which they were defined. Chrome does this as well, except for a couple cases. [...] This behavior is explicitly left undefined by the ECMAScript specification. In ECMA-262, section 12.6.4:
The mechanics of enumerating the properties ... is implementation dependent.
However, specification is quite different from implementation. All modern implementations of ECMAScript iterate through object properties in the order in which they were defined. Because of this the Chrome team has deemed this to be a bug and will be fixing it.
All browsers respect definition order with the exception of Chrome and Opera which do for every non-numerical property name. In these two browsers the properties are pulled in-order ahead of the first non-numerical property (this is has to do with how they implement arrays). The order is the same for Object.keys as well.
事实上,它不一定根据定义时的顺数输出,所有浏览器的最新版本现在都按chrome执行,先把当中的非负整数键提出来,排序好输出,然后将剩下的定义时的顺序输出。由于这个奇葩的设定,让avalon的ms-with对象排序不按预期输出了。只能强制用户不要以纯数字定义键名:
var obj = {
"first":"first",
"zoo":"zoo",
"2a":"2",
"34u":"34",
"1l":"1",
"second":"second"
};
for (var i in obj) { console.log(i+" "+obj[i]); };
IE11, chrome31, firefox23的打印如下:
first first
zoo zoo
2a 2
34u 34
1l 1
second second
for in 循环的输出顺序问题的更多相关文章
- for循环的执行顺序
一边回顾基础一边记录记录做个整理,这篇关于for循环的执行顺序: for(表达式1;表达式2;表达式3) {循环体} 第一步,先对表达式1赋初值; 第二步,判别表达式2是否满足给定条件,若其值为真,满 ...
- System.out.println与System.err.println的区别(输出顺序!!!)
System.out.println与System.err.println的区别(输出顺序!!!) 分类:java (208) (0) System.out.println与System.err.p ...
- c语言文件中关于while(!feof(fp)) 循环多输出一次的问题
文件中关于while(!feof(fp)) 循环多输出一次的问题 feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为1,否则为0. 当读到文件末尾时,文件指针并没有 ...
- JAVA优先级队列元素输出顺序测试
package code.test; import java.util.Comparator; import java.util.Iterator; import java.util.Priority ...
- while循环 格式化输出 密码本 编码的初识
第二天课程整理 while 循环 why : while ' 循环' 的意思 what : while 无限循环 how : 1.基本结构 while + 条件 循环的代码 初识循环 while tr ...
- Lodop调整打印项输出顺序 覆盖与层级
Lodop中的打印项,如果有输出在同一位置,或部分位置重叠的地方,打印项之间是怎么覆盖的呢?在JS里,按照Lodop语句打印项先后的执行顺序,先执行的先输出,后执行的后输出,如果有后面的打印项和前面的 ...
- 微软BI 之SSIS 系列 - 变量查询语句引起列输出顺序不一致的解决方法
开篇介绍 这个问题来自于 天善BI社区,看了一下比较有意思,因为我自己认为在 SSIS中处理各种类型文件的经验还比较丰富(有一年的时间几乎所有ETL都跟文件相关),但是这个问题确实之前没有特别考虑过. ...
- [转载]for循环的执行顺序
原文地址:for循环的执行顺序作者:想飞上天的美人鱼 for循环的执行顺序用如下表达式: for(expression1;expression2;expression3) { expression ...
- JavaScript循环及输出方式
好一段时间没写了,今天写一下JavaScript的循环和输出吧! 其实JavaScrip的循环跟C#.Java的循环用法是相同的. <!DOCTYPE html> <html> ...
随机推荐
- react 学习文章
生命周期 学习笔记 一些坑 项目完成后总结 理解Immutable 是否要同构如何同构 react组件最佳实践 redux集合所有的state props来源, 页面所有状态 数据的唯一来源 reac ...
- QT画图
if (0) { QApplication a(argv, args); QGraphicsScene scene; scene.setSceneRect(-300,-300,600,600); sc ...
- Delphi Xe4 游戏开发的技术选型.
asphyre 是支持 FireMonkey的. 利用Firemonkey的跨平台接口.实现 win,mac,ios. 其它方案估计就得靠 FPC 了. 好处是多了输出Andriod的可能. zeng ...
- Pythond 读写HDF5文件
HDF(Hiearchical Data Format)是一种针对大量数据进行组织和存储的文件格式,可以存储不同类型的图像和数码数据的文件格式,并且可以在不同类型的机器上传输. HDF是美国国家高级计 ...
- [STM32]HardFault 定位办法
网上关于HardFault的定位办法好多,试到了其中一种可行的 http://www.cnblogs.com/Ilmen/p/3356147.html 特此纪录.
- VS2010中使用 SpecFlow + Selenium.WebDriver
安装(VS扩展.程序包) [工具]->[扩展管理器],安装SpecFlow [工具]->[库程序包管理]->[程序包管理器控制台] PM> Install-Package Sp ...
- HDU - 6166:Senior Pan(顶点集合最短路&二进制分组)
Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory pro ...
- Xcode7 修改项目名完全攻略
1.先把整个工程文件夹名改为新的工程名. 2 .将旧项目文件夹和Tests文件名夹修改为新的名称,修改后如下图所示 3.右击 ,选择“show content package”(中文:显示包内容),看 ...
- iis部署网页时应该避免的特殊端口
1 tcpmux 7 echo 9 discard 11 systat 13 daytime 15 netstat 17 qotd 19 chargen 20 ftp data 21 ftp cont ...
- layui与layer同时引入冲突的问题
之前在项目中只有用layer,但是后来有用到了layui,结果发现同时引入这两个东东 会出现冲突的问题 导致代码运行不正常 后来网上找到了解决办法: 学习源头:http://fly.layui.com ...