Javascript中for..in循环的迭代顺序 [英] Iteration order of for..in loops in Javascript

查看:84
本文介绍了Javascript中for..in循环的迭代顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个初始化的Javascript对象

Suppose I have a Javascript object which is initialized

var letters = {q:0, t:0, o:0, b:0, y:0, n:0, u:0, m:0, p:0, 
               w:0, a:0, d:0, k:0, v:0, c:0, z:0, l:0, j:0, 
               i:0, e:0, g:0, s:0, x:0, r:0, h:0, f:0};

然后我想迭代这个对象的键

and then I want to iterate over the keys of this objects

for(var letter in letters) {
    // code goes here
}

在Firefox 3和Internet Explorer 8中,对象按对象声明(q,t,o,b, y等。)

In both Firefox 3 and Internet Explorer 8 the objects are iterated over in the order in which they are listed in the object declaration (q, t, o, b, y, etc).

我能依靠吗?假设在迭代之前我没有以任何方式修改我的对象。它是ECMAScript标准的一部分吗?有谁知道哪些浏览器按声明的顺序迭代?

Can I rely on this? Assume that I don't modify my object in any way before the iteration. Is it part of the ECMAScript standard? Does anyone know which browsers iterate in the declared order?

推荐答案

不,它不能依赖,至少不在Firefox 中:

No, it cannot be relied upon, at least not in Firefox:


A for ... in循环以任意顺序迭代对象的属性。

A for...in loop iterates over the properties of an object in an arbitrary order.

这篇关于Javascript中for..in循环的迭代顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆