Created
March 4, 2012 18:42
-
-
Save koistya/1974320 to your computer and use it in GitHub Desktop.
ES5 Complete Object 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
/** | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note! This peace of code doesn't convey a good programming style, it demonstrates minimization techniques instead.