by jashkenas · 15y ago · 0 comments
"for item in list" vs "for key, value of object" is an unfortunate necessary evil. It would be great to use the same keyword, "in", for both types of loop, but I'm afraid there's no way for us to know at compile time if "list" or "object" is really an array, or really an object.
for (var i = 0, l = list.length; i < l; i++) { ... }
And objects iterated over with: for (var key in object) { ... }
Using a "for-in" loop over a JS array isn't acceptable, for performance and semantics reasons, and neither is sniffing at runtime to determine whether the object passed is an array, or an object.Ideally, JavaScript would have supported a single iteration protocol for both arrays and objects from the get-go, but alas...