Object.clone - not quite a proposal

Isiah Meadows isiahmeadows at gmail.com
Fri Sep 25 19:29:08 UTC 2015


I'll also point out that cloning DOM elements generally won't work as
intended because DOM implementations sometimes tack on hidden properties
that aren't even seen by the engine. Also, I know that V8 adds hidden
properties to objects, which would have to be transferred as well.

And this is where persistent data structures become useful... (I recently
dropped Mori into a project because I was dealing with a ton of immutable
data, but I still wanted it to be lightweight, and I didn't need all the
bells and whistles of Immutable.js.)

On Thu, Sep 24, 2015, 20:32 Andrea Giammarchi <andrea.giammarchi at gmail.com>
wrote:

> Also, one reason I haven't used that, if you clone descriptors you do a
> shallow copy, not a deep one ;-)
>
> On Fri, Sep 25, 2015 at 2:31 AM, Andrea Giammarchi <
> andrea.giammarchi at gmail.com> wrote:
>
>> Claude, you know that's actually *my* proposal, right? :P
>> https://gist.github.com/WebReflection/9353781
>>
>> But yeah, that's the spirit!
>>
>> On Thu, Sep 24, 2015 at 8: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/20150925/9479d96d/attachment.html>


More information about the es-discuss mailing list