10 ms·
for...in loops also have the drawback of iterating over inherited properties. eg if I extended the prototype of Array with some method, then for...in would iter
by user24 16y ago
for...in loops also have the drawback of iterating over inherited properties. eg if I extended the prototype of Array with some method, then for...in would iterate over that method too.
traditional for loops incrementing until length is reached are preferable for arrays (personal style: for(var i=0,l=array.length ; i<l ; i++))
For objects, for...in with a hasOwnProperty check is preferred, I think (if you're going to be iterating over objects that is, which usually means you're using an object as a textually-indexed array, which isn't that great).
Over all a nice introduction though.
- cema 16y agousing an object as a textually-indexed array, which isn't that great Using it as a hash table is just fine.
- user24 16y agoYes, my problem was with people then wanting to iterate over said hashmap, not with using it as a hashmap in the first place. I could have worded that better, thanks. I mean if you're going to want to iterate over it, you should probably be using an array instead.