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

John J Barton johnjbarton at johnjbarton.com
Tue Nov 8 13:17:56 PST 2011


On Tue, Nov 8, 2011 at 12:49 PM, Brendan Eich <brendan at mozilla.com> 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.

Just as a point of comparison, I use this form:
   Object.keys(o).forEach( function(key) {
      body
   });

I would use |for own| if it existed, solely to avoid thinking about
overhead and because single stepping would work better.

jjb


More information about the es-discuss mailing list