Deep cloning objects defined by JSON.

Xavier MONTILLET xavierm02.net at gmail.com
Tue Jan 24 09:27:01 PST 2012


Since when you clone, you don't want things to interact but there
might still be some shared parts, how about something like that:
http://jsfiddle.net/xavierm02/5JmeU/

Instead of just cloning, you give an object and it gives you the
number you asked of objects that "deeply inherit" from the object you
gave. My code does that only for real objects (ie not arrays) but if
made native, it could probably be extended to arrays and you would
have less memory used.

And about not being able to clone functions, you can do it. All you
need is to keep:
- its name
- its action
- its scope

So if you define another function that simply calls the one you wanted
to clone, you have a cloned function because you keep the scope and
the action. And if you really want the name, you can use new Function
to keep it.

And for a clone that would clone anything, it could be something like that:
http://jsfiddle.net/xavierm02/pjwvV/

But I'm not sure there would be usecases for it.

On Tue, Jan 24, 2012 at 2:33 AM, Russell Leggett
<russell.leggett at gmail.com> wrote:
>>
>> As part of the HTML5 specification, there is the structured clone
>> algorithm
>>
>>
>> Which is also incapable of copying a function.
>>
>
> True, but the original suggestion was limited only to the json subset.
> Structured cloning is strictly more powerful than that, and implemented in
> multiple browsers so I thought it was relevant to this conversation.
>
> - Russ
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list