for..in, hasOwnProperty(), and inheritance

Felipe Gasper felipe at felipegasper.com
Tue Nov 8 11:05:50 PST 2011


On 11/8/11 12:37 PM, Axel Rauschmayer wrote:
> What’s the use case?

I thought I gave a pretty reasonable one before, but just in case:

In YUI, it’s impossible to use this otherwise-useful pattern:
---------
var base_config = { width: "600px" };
…
var my_config = Object.create(base_config);
my_config.visible = false;
var widget = new Y.Widget(my_config);
---------
In the example above, YUI will not see the “width” property because
YUI rejects all inherited properties when it iterates through the
configuration hash.

>
> Don’t forget that whenever you set a property, you only ever modify the
> first object in the prototype chain.

Right…that’s why gave() walks the prototype chain unless it finds the 
property *on* the object itself.

>
> The “own property” debate mainly exists, because objects are (ab)used as
> dictionaries. Then you don’t want inherited entries such as "toString".

I disagree. That’s actually the crux of what I’m getting at.

IMO, you actually do want “inherited entries”; what you don’t want are 
specifically those things inherited from Object.prototype. Weeding out 
*all* inherited properties assumes that there is no legitimate use case 
for objects inheriting from other objects…which defeats the whole 
purpose of stuff like Object.create().

-FG


More information about the es-discuss mailing list