for..in, hasOwnProperty(), and inheritance

Brendan Eich brendan at mozilla.com
Tue Nov 8 12:19:47 PST 2011


On Nov 8, 2011, at 12:12 PM, Luke Smith wrote:

>> 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.

Yup, that's a fact. However it contradicts Felipe's belief that only Object.prototype might-be-enumerable properties need to be excluded, which was my point.


> Can you elaborate on "we should support abstracting over user-defined and built-in functions"?

Abstract over means I can for-in an instance of either a built-in or (such a) user-defined constructor and not see methods. I can self-host or wrap Date without methods showing up.

Yes, ES5 supports Object.defineProperty etc. so I can make the WrappedDate.prototype methods non-enumerable, if I can target ES5. But the general point stands: enumeration up the prototype chain either is a bug, or you want no enumerable prototype properties (except perhaps those added by one privileged library), or you want a more elaborate protocol such as Felipe's gave/giver one.


> 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.

That might be handy but Felilpe's gave/giver protocol still seems necessary in general (if you can't assume no one has decorated intermediate prototypes with enumerable properties that should not be considered).

The flattening idea reminds me of traits (http://traitsjs.org/), since you might want different conflict-handling policies in case of shadowing. The default could shadow.

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


More information about the es-discuss mailing list