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

David Bruant bruant.d at gmail.com
Tue Nov 8 14:16:29 PST 2011


Le 08/11/2011 21:49, Brendan Eich a écrit :
> The recommended practice when writing for-in loops in JS today is to 
> write:
>
>   for (i in o) {
>     if (o.hasOwnProperty(i)) {
> /body/
>     }
>   }
As said in the thread your forked from, this practice is often 
recommended to not enumerate Object.prototype methods (rather than 
enumerating only own properties)

> 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.
Why would developers use for own while they currently do not use 
hasOwnProperty? I really am skeptical on this.
Enumeration of own enumerable properties can be done with Object.keys. 
Why not encourage people to use this?
Are you also proposing an addition of for own (... of ...) if for-own-in 
and for-of are both accepted?

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


More information about the es-discuss mailing list