Object.getOwnPropertyDescriptors(O) ? // plural
Andrea Giammarchi
andrea.giammarchi at gmail.com
Wed Mar 5 12:30:31 PST 2014
Thanks for clarification Tom, I've also added the main two _features_ this
method in the first gist I've posted but here a quick recap:
```javascript
// for shallow object copy
var shallowCopy = Object.create(
Object.getPrototypeOf(origin),
Object.getOwnPropertyDescriptors(origin)
);
// for traits / mixin
Object.defineProperties(
target,
Object.getOwnPropertyDescriptors(origin)
);
// respecting ES5 symmetry
Object.defineProperty(O, name, descriptor);
descriptor as Object.getOwnPropertyDescriptor(O, name)
Object.defineProperties(O, descriptors);
descriptors as Object.getOwnPropertyDescriptors(O)
```
Hope this one help presenting the what, the why, and the how.
Best Regards
On Wed, Mar 5, 2014 at 10:39 AM, Tom Van Cutsem <tomvc.be at gmail.com> wrote:
> Object.getOwnPropertyDescriptors(obj) makes sense to me. As noted
> somewhere upstream, it's symmetrical to Object.defineProperties.
>
> When I designed traits.js, which was heavily based on property
> descriptors, I even added it to the library's public API, under the
> slightly shorter name Object.getOwnProperties(obj), cf. <
> http://traitsjs.org/api.html>
>
> Brendan asked me whether Object.getOwnPropertyDescriptors would
> necessitate a derived trap for proxies. I think the answer is no, just like
> Object.defineProperties(proxy) triggers just the proxy's
> getOwnPropertyNames trap, and then the defineProperty trap for each
> property, Object.getOwnPropertyDescriptors(proxy) would trigger the
> getOwnPropertyNames trap, followed by calls to the getOwnPropertyDescriptor
> trap for each individual property.
>
> Cheers,
> Tom
>
>
>
>
> 2014-03-05 1:38 GMT+01:00 Andrea Giammarchi <andrea.giammarchi at gmail.com>:
>
>> You are right, I've realized it after posting ... `Object.assign(target,
>> source)` via `Get` and `Put` is basically the equivalent of a classic
>> `for/in` with `hasOwnProperty` check ... well, once again, in ES6
>> `Object.getOwnPropertyDescriptors(O)` seems to be a solution for other
>> cases too (shallow copy of properties, shallow clone, not just for/in) plus
>> yes, `Object.mixin(target, source)` might be simplified as well via this
>> proposal, that's a good point ... so 3 problems solved at once! :-)
>>
>>
>> On Tue, Mar 4, 2014 at 4:02 PM, Claude Pache <claude.pache at gmail.com>wrote:
>>
>>>
>>>
>>> Le 5 mars 2014 à 00:18, Andrea Giammarchi <andrea.giammarchi at gmail.com>
>>> a écrit :
>>>
>>>
>>> Also please note that this proposal simplifies [Object.assign(target,
>>> source)](
>>> https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign)
>>> too since latter one is basically:
>>>
>>> ```javascript
>>> Object.defineProperties(
>>> target,
>>> Object.getOwnPropertyDescriptors(source)
>>> );
>>> ```
>>>
>>>
>>> No. `Object.assign` (1) uses the semantics of the assignment operator
>>> `=` (for example, it makes a difference in case of setters), and (2) copies
>>> only enumerable properties. Did you think about `Object.mixin`?
>>>
>>> --Claude
>>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140305/950e4c54/attachment.html>
More information about the es-discuss
mailing list