This was the age of the question , and I know the usual reasons not to use, for..inor perhaps even objects, when there is any kind of ordering, but I recently came across this article from MDC in a delete statement.
Browser issues
Although ECMAScript makes the iterative order of objects dependent on the implementation, it might seem that all major browsers support the iteration order based on the earliest added property that appears first (at least for properties not belonging to the prototype). However, in the case of Internet Explorer, when you use delete on a property, there is some kind of confusing behavior that allows other browsers to use simple objects, such as object literals, as ordered associative arrays. In explorer, while the property value is indeed set to undefined, if a property with the same name is added later, the property will be iterated at its old position - not at the end of the iteration sequence, as you would expect after deleting the property, and then added it back.
So, if you want to model an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for keys, and the other for values), or build an array of single-property objects, etc.
It seems that most major browsers - Chrome, Safari, Firefox, and Opera list the properties of objects in insertion order. This is a quick test to confirm this. It may not work on IE, but I do not have access to IE to check this, unfortunately. So IE also keeps order (6/7) when repeating properties with for..in, assuming we don't delete anything from the object?
, for..in , , Array.prototype , Object.prototype. , , , for..in , : , Array.prototype, .
:
, hasOwnProperty
for(var index in object) {
if(object.hasOwnProperty(index)) {
..
}
}
ES5 . , , .
Object.defineProperty(Array.prototype, "size", {
enumerable: false,
configurable: false,
get: function() { return this.length; },
set: undefined
});
, - , for..in :
var values = [1, 2, 3];
for(var i in values) {
values[i];
}
, , . , , IE ? , .