Nuking misleading properties in `Object.getOwnPropertyDescriptor`

David Bruant bruant.d at gmail.com
Thu Mar 14 02:12:22 PDT 2013


Le 14/03/2013 08:51, Tom Van Cutsem a écrit :
> [+Allen]
>
> 2013/3/13 Nathan Wall <nathan.wall at live.com <mailto:nathan.wall at live.com>>
>
>     However, as a matter of principle, my argument is that
>     `Object.getOwnPropertyDescriptor` should, at the bare minimum,
>     return a descriptor that can be known to work in
>     `Object.defineProperty`.  If `Object.defineProperty` doesn't
>     accept it, then you `getOwnPropertyDescriptor` didn't really give
>     me a valid descriptor.
>
>     I think that this behavior (1) limits the creativity of developers
>     to define properties like `Object.prototype.get`, (2) is a
>     potential stumbling block, (3) has no real benefit -- really,
>     there's not anything positive about this behavior, and (4) forces
>     developers who want to support `Object.prototype.get` to add an
>     extra layer of cleaning before using `defineProperty`.
>
>
> While the monkey-patching of Object.prototype ("don't do that!") is 
> still the culprit, I agree that it would have been better if 
> defineProperty looked only at "own" properties of the descriptor.
In a previous message, Brandon Benvie mentioned he uses inheritance to 
reuse a property descriptor [1] (I think there was another quote of him, 
but I can't find it now). I can imagine it's a used pattern.

> I almost always think of descriptors as "records" rather than 
> "objects". Similarly, perhaps Object.getOwnPropertyDescriptor should 
> have returned descriptors whose [[prototype]] was null.
>
> It's true that Reflect.getOwnPropertyDescriptor and 
> Reflect.defineProperty give us a chance to fix this. I'm just worried 
> that these differences will bite developers that will assume that 
> these methods are identical to the Object.* versions.
I doubt differences would be a good idea.

Maybe an idea would be for Object.defineProperty to call 
Attributes.@@iterate is user-defined so that a user can restrict what 
property descriptor properties are being traversed.
If that's too heavy of a refactoring, maybe an ES6 map could be accepted 
as the 3rd argument of Object.defineProperty (with maps semantics, not 
objects semantics). This way, one could write the copy function as:

     function copy(from, to) {
         for (let name of Object.getOwnPropertyNames(from)){
             let desc = Object.getOwnPropertyDescriptor(from, name);
             desc[@iterator] = ownIterator; // is that the proper 
syntax? I'm a bit lost :-/
             Object.defineProperty(to, name, new Map(desc));
         }
     }

ownIterator only iterates over own properties as its name indicates, so 
the Map will only list that. The extra map allocation isn't that big of 
a deal since it is very short-lived. It could be shared and cleared 
across iterations if necessary.

Nathan, how do you feel about such a solution?

David

[1] https://mail.mozilla.org/pipermail/es-discuss/2012-November/026081.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130314/caca49a8/attachment.html>


More information about the es-discuss mailing list