`I'm reading 'JavaScript: the definitive guide' and I'm getting hung up on an example:
“you can use code like the following to copy the names of all object properties into an array"
var o = {x:1, y:2, z:3};
var a = [], i = 0;
for(a[i++] in o) /* empty */;
"following the code above with this line enumerates the array indexes 0, 1, and 2"
for(i in a) console.log(i);
Can someone please explain to me like I'm five how the first for/in
loop works? It seems to me a[i++]
would evaluate to 1
on the first time through the loop, not 0
.