Mutating an object in an iterator
by steveukx
HTML
<p>Checking how object iterators work:</p>
<p>Run through an object mutating the object in each iteration. The set of keys used for the iterator should be the keys in the object when the iteration started and not a live list.</p>
CSS
* { font: 10pt/1.5 Verdana, Helvetica; }
JavaScript
var obj = {
foo: "bar",
bar: "foo"
};
document.body.appendChild(document.createElement('p'))
.appendChild(
document.createTextNode("Starting: " +JSON.stringify(obj))
);
for(var key in obj) {
obj[Math.random()] = 1;
delete obj.foo;
document.body.appendChild(document.createElement('div'))
.appendChild(
document.createTextNode(key + ": " +JSON.stringify(obj))
);
}
document.body.appendChild(document.createElement('p'))
.appendChild(
document.createTextNode("Ending: " +JSON.stringify(obj))
);