简体   繁体   English

for..in循环是否跟踪对象的属性顺序?

[英]Does for..in loop keeps track of orders of its properties for Object?

I'm a beginner for programming in general, and JavaScript is my first language. 我是一般编程的初学者,而JavaScript是我的第一语言。 While I was studying, I faced with this concern, and I'm not sure if my concern is appropriate. 在学习期间,我遇到了这种担忧,并且不确定自己的担忧是否适当。

I thought for..in loop does not keep track of orders when we loop through an Object, because, unlike Arrays, Object's properties are not index based. 我认为for..in循环在我们遍历对象时不会跟踪顺序,因为与数组不同,对象的属性不是基于索引的。

This leads me to question my work on recreating some of the methods from Underscore.js such as _.each and _.reduce 这使我对我从_.each重新创建一些方法(例如_.each_.reduce

Below is my attempts to recreate those methods. 下面是我尝试重新创建这些方法的尝试。

var each = function(list, iteratee) {
    if (Array.isArray(list)) {
        for (var i = 0; i < list.length; i++) {
            iteratee(list[i], i, list);
        }
    } else if (list.constructor === Object) {
        for (var key in list) {
            iteratee(list[key], key, list);
        }
    }
};

var reduce = function(list, iteratee, memo){  
  var memoUndefined = arguments.length < 3;
  each(list, function(elem, index, list){
    if (memoUndefined) {
      memoUndefined = false;
      memo = elem;
    } else {
        memo = iteratee(memo, elem, index, list);
    }
  });
  return memo;
};

They seem to work correctly, such that works on both Array and Object as its list , with or without memo supplied to it. 他们似乎正常工作,这样在两个数组和对象作为其作品list ,有无memo提供给它。

However, what makes me wonder is that how does my each function keep track of the orders of properties it is iterating over? 但是,令我感到奇怪的是,我的each函数如何跟踪要迭代的属性的顺序? How would it make sure the orders of the properties being looped is always consistent? 如何确保要循环的属性的顺序始终一致? It makes sense for Arrays because it's properties are index. 这对数组有意义,因为它的属性是索引。 But Object's is not. 但是对象不是。 I believe this might have a potential issue when being implemented in a function like my reduce above. 我相信在像我上面的reduce这样的功能中实现时,这可能会存在潜在的问题。 If what iteratee does in the future is just simple addition or subtraction it might be fine, but what about multiplications or division? 如果将来iteratee所做的只是简单的加法或减法,那很好,但是乘法或除法呢? The orders of values each will iterate will be crucial. each值的迭代顺序至关重要。 Is my concern appropriate? 我的关注合适吗?

The ECMAScript ES5 specification specifically says that the order of properties is not guaranteed for a for/in loop. ECMAScript ES5规范特别指出,对于for/in循环,不能保证属性的顺序。 From section 12.6.4 in the ES5 specification: 根据ES5规范的12.6.4节

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified. 枚举属性的机制和顺序(第一个算法中的步骤6.a,第二个算法中的步骤7.a)未指定。

Though, in most browser implementations of ES5, properties are iterated in the order they were created. 但是,在大多数ES5浏览器实现中,属性都是按照创建顺序进行迭代的。

I have read that it has been proposed to document the order of properties for for/in as the order created in the emerging ES6 specification. 我已经读到,有人建议将for / in的属性顺序记录为新兴的ES6规范中创建的顺序。

I haven't found this specific wording in the ES6 draft, but there is this note about Object.keys() : 我在ES6草案中没有找到这种特定的措词,但是有关于Object.keys()注释:

If an implementation defines a specific order of enumeration for the for-in statement, the same order must be used for the elements of the array returned in step 4. 如果实现为for-in语句定义了枚举的特定顺序,则必须对步骤4中返回的数组元素使用相同的顺序。

Keep in mind that even if the order is always a specific way, there are no features for modifying the order. 请记住,即使订单始终是特定的方式,也没有用于修改订单的功能。 So, if you really care about order of properties, then it is likely that a for/in loop on an object is not really the right way to store/access an ordered set of properties. 因此,如果您确实关心属性的顺序,则对象上的for/in循环可能不是存储/访问有序属性集的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM