Thanks, combining this code with Object.create seems to be working
fine for all test cases I have tried so far. I could even copy DOM
nodes.

global.copy = function(refObject) {  var newObject =
Object.create(refObject.proto);  var property;  var descriptor;
  var properties = Object.getOwnPropertyNames(refObject);
  properties.forEach( function(property) {    descriptor =
Object.getOwnPropertyDescriptor(refObject, property);
Object.defineProperty(newObject, property, descriptor)  });
  return newObject;}
On Wed, Nov 30, 2011 at 10:11 PM, Jake Verbaten <rayn...@gmail.com> wrote:
> https://gist.github.com/1410832
>
> function clone(o) {
>   var pds = {};
>   Object.getOwnPropertyNames(o).forEach(function _eachName(name) {
>     pds[name] = Object.getOwnPropertyDescriptor(o, name);
>   });
>
>   return Object.create(Object.getPrototypeOf(o), pds);
> }
>
> The naive clone simply gets its own property descriptors and the prototype
> and returns a new object thats the combination of the two.
>
>
> There are a ton of edge cases that break here. Like no deep copying, no
> copying array / functions, no copying proxies, etc
>
>
> I highly recommend you avoid the "holy grail" of the perfect deep copy
> because it's a nightmare.
>
>
> Stick to shallow clones.
>
>
>
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/jsmentors@jsmentors.com/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/jsmentors@googlegroups.com/
>
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to