Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 9, 2012 02:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/3300375 to your computer and use it in GitHub Desktop.
Save Raynos/3300375 to your computer and use it in GitHub Desktop.
A proper clone
// 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
}
@domenic
Copy link

domenic commented Aug 9, 2012

Nice trick passing a "third argument" to getPropertyDescriptor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment