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> ...
随机推荐
- 由PostgreSQL的区域与字符集说起(转)
转自:http://blog.chinaunix.net/uid-354915-id-3502551.html 由PostgreSQL的区域与字符集说起 一.PostgreSQL的区域区域属性有以下几 ...
- WEKA中的数据预处理
数据预处理包括数据的缺失值处理.标准化.规范化和离散化处理. 数据的缺失值处理:weka.filters.unsupervised.attribute.ReplaceMissingValues. 对于 ...
- TCP服务器端口数,最大连接数以及MaxUserPort的关系辨真
原文连接:http://www.jianshu.com/p/4a58761d758f 关于TCP服务器最大并发连接数有一种误解就是"因为端口号上限为65535,所以TCP服务器理论上的可承载 ...
- prefixfree.min.js 的用途
简单而言,这东西就是自动加前缀的,在link中,或是style中,或是dom元素的style中书写CSS3 code,或是jQuery .css()方法此脚本会自动补上需要的前缀,让响应的浏览器支持该 ...
- R 语言赋值运算符:`<-` , `=`, `<<-`
<- 与 = 间的区别 <- 与 = 在大部分情况下是应该可以通用的.并且,相对于 <<- 运算符,它们的赋值行为均在它们自身的环境层(environment hierarch ...
- 20165222 预备作业3 Linux安装及学习
一,安装Linux操作系统 一开始下载ubuntu的时候比较麻烦,然后用同学发给我的,才得以安装.根据教程,也没出现什么问题,只是下载的比较麻烦. 二,关于Linux的学习 1,Linux系统简介和 ...
- C# 数组、ArrayList和List三者的区别
在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. ...
- HttpWebRequest传值
From:发送方 class Program { static void Main(string[] args) { string strId = "zhangsan"; &quo ...
- matlab的fda工具使用方法
MATLAB中用FDATool设计滤波器及使用 该文章讲述了MATLAB中用FDATool设计滤波器及使用. 1. 在Matlab中键入fdatool运行Filter Design and Analy ...
- Spring源码学习之:ClassLoader学习(5)-自测
[一]测试目的(ClassLoader的作用) 1:测试涉及三个jar包,nonbankcard-configure-0.0.1-SNAPSHOT.jar,nonbankcard-persist-0. ...