Skip to content

Instantly share code, notes, and snippets.

@edgarberm
Last active August 29, 2015 14:13
Show Gist options
  • Save edgarberm/f94f9b57250b10c52c2a to your computer and use it in GitHub Desktop.
Save edgarberm/f94f9b57250b10c52c2a to your computer and use it in GitHub Desktop.
The following code creates a copy of a given object.
function copy(o){
var copy = Object.create( Object.getPrototypeOf(o) );
var propNames = Object.getOwnPropertyNames(o);
propNames.forEach(function(name){
var desc = Object.getOwnPropertyDescriptor(o, name);
Object.defineProperty(copy, name, desc);
});
return copy;
}
var o1 = {a:1, b:2};
var o2 = copy(o1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment