Object.clone - not quite a proposal

Jordan Harband ljharb at gmail.com
Thu Sep 24 19:11:46 UTC 2015


Cloning is not a trivial matter. You may be interested in these previous
discussions before discussing further:

 - https://esdiscuss.org/topic/the-structured-clone-wars
 - https://esdiscuss.org/topic/structured-clones
 -
https://esdiscuss.org/topic/structured-cloning-transfering-of-promises-and-streams
 - https://esdiscuss.org/topic/how-would-we-copy-anything
 - https://esdiscuss.org/topic/deep-cloning-objects-defined-by-json

- Jordan

On Thu, Sep 24, 2015 at 11:42 AM, Jeremy Darling <jeremy.darling at gmail.com>
wrote:

> The code was only meant to be a better explanation of the goal, but yes it
> would have to take into account getters, setters, etc...
>
> Actually it was a copy/paste edit of some code we use for cloning
> (deserialized) JSON objects for API reproduction.  There is an omit stage
> that was removed and since it was originally designed around JSON
> structures there is no built in support for custom properties, getters,
> setters.
>
> DOM nodes I'm not sure how you would account for.  In all actuality I
> would think they would throw an error, but maybe they shouldn't.  Magically
> appearing textboxes could be a bad thing :D
>
> On Thu, Sep 24, 2015 at 1:21 PM, Claude Pache <claude.pache at gmail.com>
> wrote:
>
>>
>> Le 24 sept. 2015 à 19:58, Andrea Giammarchi <andrea.giammarchi at gmail.com>
>> a écrit :
>>
>> Last, but not least, one does not simply assign properties in JavaScript.
>> If you want to clone you don't want to miss possible get/set accessors in
>> the process, you don't want to miss getOwnPropertyNames and
>> getOwnpropertySymbols neither, so the last loop doesn't work as proposal.
>>
>> ```js
>> // simplified version for regular objects only
>> // basically just the correct substitute of your last temp logic
>> Object.clone = function (src) {
>>   return Object.setPrototypeOf(
>>     Reflect.ownKeys(src).reduce((o, k) => {
>>       var d = Object.getOwnPropertyDescriptor(src, k);
>>       if (
>>         o.hasOwnProperty.call(d, 'value') &&
>>         /function|object/.test(typeof d.value)
>>       ) d.value = Object.clone(d.value);
>>       return Object.defineProperty(o, k, d);
>>     }, {}),
>>     Object.getPrototypeOf(src)
>>   );
>> };
>> ```
>>
>>
>> ```js
>> Object.clone = function (src) {
>>     return Object.create(Object.getPrototypeOf(src),
>> Object.getOwnPropertyDescriptors(src))
>> }
>> ```
>>
>> where  the definition of `Object.getOwnPropertyDescriptors()` is left as
>> an exercise to the reader.
>>
>>
>> —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/20150924/bd0c5bc3/attachment-0001.html>


More information about the es-discuss mailing list