for..in, hasOwnProperty(), and inheritance

Luke Smith lsmith at lucassmith.name
Tue Nov 8 12:12:20 PST 2011



> ------------------------------------------------------------------------
>
> 	Brendan Eich <mailto:brendan at mozilla.com>
> November 8, 2011 11:59 AM
>
>
> On Nov 8, 2011, at 11:48 AM, Felipe Gasper wrote:
>
>> Actually, have you ever seen a use case of wanting to prevent iteration through inherited properties *other* than Object.prototype? (Besides using for..in on Array objects.)
>
> Sure. People make "classes" by defining function C(){} and decorating C.prototype.method1 = function(...){...}, etc. A for-in loop on (new C) will see all the methods, unlike the case with built-in constructors. That is an unwanted abstraction break: we should support abstracting over user-defined and built-in functions.
I believe people decorate a class prototype directly because they always 
have, and because defineProperties wasn't available for them to declare 
prototype methods as not enumerable, not because they explicitly want 
class proto methods to be enumerable.

Can you elaborate on "we should support abstracting over user-defined 
and built-in functions"?
>
>
>> All of the examples I’ve ever seen of the problems that ensue from for..in iteration and prototypes stem from extending Object.prototype. Why not, then, specifically address that problem rather than preventing all iteration through inherited properties?
>
> I think you're focusing too narrowly. I agree with Jake: make a copy into a single object, and access it (if it's a config dictionary) with a stylized API. Sounds like you could make YUI happy that way.
Copying loses the benefit of shared defaults via prototype.  I would 
rather see a method to flatten an object before enumeration than lose 
the flexibility of shared state.

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111108/6d6b1f68/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compose-unknown-contact.jpg
Type: image/jpeg
Size: 770 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111108/6d6b1f68/attachment.jpg>


More information about the es-discuss mailing list