Object.getOwnPropertyDescriptors(O) ? // plural

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun Mar 2 17:43:03 PST 2014


Brandon I take your answer as +1, thanks.

I've also "gisted" a possible/basic polyfill here:
https://gist.github.com/WebReflection/9317065

Cheers


On Sun, Mar 2, 2014 at 5:16 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> actually, since `Object.getOwnPropertyDescriptor` accepts `Symbols` too as
> second argument, the equivalent with current ES6 would be even more prolix
> than that
>
> ```javascript
>
> var shallowCopy = Object.create(
>   Object.getPrototypeOf(originalObject),
>   Object.getOwnPropertyNames(originalObject).concat(
>   Object.getOwnPropertySymbols(originalObject)
>   ).reduce(function (descriptors, name) {
>     descriptors[name] = Object.getOwnPropertyDescriptor(
>       originalObject, name
>     );
>     return descriptors;
>   }, {})
> );
>
> ```
>
> assuming Symbols will be definable through descriptors ... all this VS
>
> ```javascript
> var shallowCopy = Object.create(
>   Object.getPrototypeOf(originalObject),
>   Object.getOwnPropertyDescriptors(originalObject)
> );
> ```
>
> which I believe is a win.
>
> Thanks
>
>
> On Sun, Mar 2, 2014 at 5:01 PM, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>> I wonder if by any chance this could sneak into ES6 ... we have only
>> singular version here:
>>
>> https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.getownpropertydescriptor
>>
>> **rationale**
>> The easiest way to create a shallow copy of a generic object could be:
>>
>> ```javascript
>> var shallowCopy = Object.create(
>>   Object.getPrototypeOf(originalObject),
>>   Object.getOwnPropertyDescriptors(originalObject)
>> );
>> ```
>>
>> Today what we have to do this instead:
>>
>> ```javascript
>> var shallowCopy = Object.create(
>>   Object.getPrototypeOf(originalObject),
>>   Object.getOwnPropertyNames(originalObject).reduce(
>>   function (descriptors, name) {
>>     descriptors[name] = Object.getOwnPropertyDescriptor(
>>       originalObject, name
>>     );
>>     return descriptors;
>>   }, {})
>> );
>> ```
>>
>> I see latter pattern as sort of unnecessary overhead that could be also
>> much faster in core and polyfilled in the meanwhile.
>>
>> Thoughts ?
>>
>> Cheers
>>
>>
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140302/f3689bab/attachment.html>


More information about the es-discuss mailing list