Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active August 29, 2015 13:57
Show Gist options
  • Save slikts/9582952 to your computer and use it in GitHub Desktop.
Save slikts/9582952 to your computer and use it in GitHub Desktop.
Object cloning using property descriptors
function clone(obj, deep) {
if (!(obj instanceof Object)) {
return obj;
}
var descriptors = {};
Object.getOwnPropertyNames(obj).forEach(function(name) {
var prop = Object.getOwnPropertyDescriptor(obj, name);
if (deep) {
prop.value = clone(prop.value);
}
descriptors[name] = prop;
});
return Object.create(Object.getPrototypeOf(obj), descriptors);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment