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> ...
随机推荐
- 采用Spring管理Bean和依赖注入
1. 实例化spring容器和从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] ApplicationContext ctx ...
- WindowManager实现悬浮可拖动效果
现在360手机卫士有个流量统计的效果,开启流量统计后,在桌面上会出现一个显示流量的窗体,在任何界面都可以自由拖动. 模仿这个功能,做了一个统计手机信号强度的Demo, 界面效果如下: 从上面的截图可以 ...
- struts1和struts2原理解析
1.struts1和struts2 是2个完全不同的框架 其实struts2核心就是 webwork框架 struts1以ActionServlet作为核心控制器,由ActionServlet负责拦截 ...
- 大容量txt数据导入SQL Server助攻记
小伙伴们有个数据竞赛,提供的数据是944MB大小的TXT数据文档,导入SQL遇到一些麻烦.于是帮着解决,顺便也熟练了SQL Server的一些操作----- 打开如此大的txt需要的时间很长,而且不全 ...
- 【转】C# Socket编程(4)初识Socket和数据流
[转自:https://www.cnblogs.com/IPrograming/archive/2012/10/15/CSharp_Socket_4.html] 经过前面基础知识作为背景,现在对Soc ...
- NSArray倒序输出的方法
NSMutableArray *array = [NSMutableArray arrayWithObjects:",nil]; NSArray* reversedArray = [[arr ...
- ACM学习历程—51NOD 1685 第K大区间2(二分 && 树状数组 && 中位数)
http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...
- Oracle恢复drop误删除的表和建用户操作
一.表的恢复 对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: 1.从flash back里查询被删除的表 select * from r ...
- quartz框架实现定时任务举例
简单的定时任务功能可以通过原生的java.util.Timer定义执行时间规则.继承java.util.TimeTask来定义执行逻辑来实现,更方便的是利用开源的quartz框架,只需定义几个spri ...
- Does Windows have a limit of 2000 threads per process?
http://blogs.msdn.com/b/oldnewthing/archive/2005/07/29/444912.aspx Often I see people asking why the ...