Created
August 9, 2012 02:24
-
-
Save Raynos/3300375 to your computer and use it in GitHub Desktop.
A proper clone
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
// example: http://jsfiddle.net/QVqHs/ | |
function clone(o) { | |
return Object.create( | |
Object.getPrototypeOf(o), | |
getOwnPropertyDescriptors(o) | |
) | |
} | |
function getOwnPropertyDescriptors(o) { | |
return Object.keys(o).reduce(getPropertyDescriptor.bind(o), {}) | |
} | |
function getPropertyDescriptor(acc, key) { | |
acc[key] = Object.getOwnPropertyDescriptor(this, key) | |
return acc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice trick passing a "third argument" to
getPropertyDescriptor
.