The ecma262 specification has a heading with the name 13.7.5.15EnumerateObjectProperties As we can understand from its name, it is responsible for enumirating properties in objects. This "semi-algorithm" says that objects must follow the following rules (I will not list all, because the rest are obvious):
- The iterator's throw and return methods are null and are never invoked.
If I understand everything correctly, then the iterator should return null, and it returns undefined when trying to call these methods. Although it probably means that the iterator does not have these methods and they are empty as a fact and do not have the value of emptiness in the form of type null.
- Properties of the target object may be deleted during enumeration. A property that is deleted before it is processed by the iterator's next method is ignored.
Yes, the property can indeed be deleted during the enumeration (meaning for...in/of).
/// Deletion properties on object
var arr = {a:111,b:222,c:333,d:444,e:555,f:666,g:777,h:888,i:999};
for(var item in arr){
if(item == "c") delete arr["c"];
if(item == "e") delete arr["e"];
if(item == "h") delete arr["h"];
console.log(arr[item]); /// when we will be on property "c", we can delete it, it's will accomplished successfull
}
console.log(arr);
But the second sentence confuses me. It is said that a property that is deleted before the iterator process the next method will be ignored. Can you demonstrate such an example? For some reason I fell into a confusion and could not.
- If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration
This is true (at least in chrome):
/// Addition properties on object
var arr = {a:111,b:222,c:333,d:444,e:555,f:666,g:777,h:888,i:999};
for(var item in arr){
if(item == "c") arr["c1"] = 1;
if(item == "e") arr["e1"] = 1;
if(item == "h") arr["h1"] = 1;
console.log(arr[item]);
}
console.log(arr);
- EnumerateObjectProperties must obtain the own property keys of the target object by calling its [[OwnPropertyKeys]] internal method.
And in my opinion one of the important statements in this paragraph. As you understand, [[OwnPropertyKeys]] defines the order in which properties are listed in an object. Therefore, we look at the second paragraph of our header:
The mechanics and order of enumerating the properties is not specified but must conform to the rules specified below.
Uhm? What? Can you explain what the hell?