Skip to content

Instantly share code, notes, and snippets.

@ollym
Created January 23, 2012 12:08
Show Gist options
  • Save ollym/1662767 to your computer and use it in GitHub Desktop.
Save ollym/1662767 to your computer and use it in GitHub Desktop.
ECMA5 Object Cloning
Object.clone = function(obj) {
return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyNames(obj).reduce(function(memo, name) {
return (memo[name] = Object.getOwnPropertyDescriptor(obj, name)) && memo;
}, {}));
}
@Raynos
Copy link

Raynos commented Jan 23, 2012

And in ES6

Object.clone = function clone(obj) {
  return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
};

@id0Sch
Copy link

id0Sch commented Apr 3, 2016

that's a shallow clone and not a deep one tho, right?

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