for own(...) loop (spin-off from Re:, hasOwnProperty(), and inheritance)

Quildreen Motta quildreen at
Tue Nov 8 14:03:20 PST 2011

On 08/11/11 18:49, Brendan Eich wrote:
> The recommended practice when writing for-in loops in JS today is to 
> write:
>   for (i in o) {
>     if (o.hasOwnProperty(i)) {
> /body/
>     }
>   }
> Although many JS developers do not follow the recommendation (out of 
> ignorance or intentionally, doesn't matter).
> Should provide sugar for the recommended pattern? To make it 
> compose with declarations and destructuring in the for head, it should 
> use a contextual keyword immediately after 'for':
>   for own (i in o) {
> /body/
>   }
> This is a small thing but it might pay off in the long run.
Isn't that just:

Object.keys(o).forEach(function(i){ body })

Iirc, that's faster than a loop with filter in v8 due to the 
aggressive function inlining, not sure about SpiderMonkey. It also reads 
better and it's more composable -- for me, at least.

I do use keep `var keys = Object.keys' and other hand aliases though.

It still seems to me it's overkill to add special syntax (and a new 
reserved word, yuck!) to something that isn't really worth it, given the 
Object API already provides those methods.

Also, are Object.values and Object.items standardised in 
They're quite useful?

> /be
> _______________________________________________
> es-discuss mailing list
> es-discuss at

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list