Skip to content

Instantly share code, notes, and snippets.

@koistya
Created March 4, 2012 18:42
Show Gist options
  • Save koistya/1974320 to your computer and use it in GitHub Desktop.
Save koistya/1974320 to your computer and use it in GitHub Desktop.
ES5 Complete Object Clone
/**
* ES5 Complete Object Clone
*
* @param {Object} Object to clone
* @return {Object} Cloned object
*/
function (obj, tmp /* placeholder */) {
// import Object.*
with(Object)
// get property names
return getOwnPropertyNames(obj)
// iterate names
.map(tmp = function(name) {
// get descriptors
tmp[name] = getOwnPropertyDescriptor(obj, name);
}),
// get proto and create clone
create(getPrototypeOf(obj), tmp);
}
@koistya
Copy link
Author

koistya commented Mar 4, 2012

Note! This peace of code doesn't convey a good programming style, it demonstrates minimization techniques instead.

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