Last active
August 29, 2015 14:13
-
-
Save edgarberm/f94f9b57250b10c52c2a to your computer and use it in GitHub Desktop.
The following code creates a copy of a given object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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