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

Quildreen Motta quildreen at gmail.com
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 ES.next 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 for..in 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 ES.next. 
They're quite useful?

>
> /be
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111108/1346f91e/attachment-0001.html>


More information about the es-discuss mailing list