Skip to content

Instantly share code, notes, and snippets.

@h2non
Last active December 15, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h2non/5197043 to your computer and use it in GitHub Desktop.
Save h2non/5197043 to your computer and use it in GitHub Desktop.
Simple underscore function to create secure "inmutable" (recursive deep copy) JavaScript Objects
/**
* Simple underscore function to create secure inmutable (recursive deep copy) JavaScript Objects
* @param {Object/Array} obj Target Object. Required
* @param {Number} depth Depth level. Defaults to 1
* @return {Object/Array) Super inmutable new Object
* @method deepClone
*/
_.deepClone = function deepClone(obj, depth) {
var clone, key;
depth = depth || 1;
if (typeof obj !== 'object' || obj === null) { return obj; }
if (_.isString(obj)) { return obj.splice(); }
if (_.isDate(obj)) { return new Date(obj.getTime()); }
if (_.isFunction(obj.clone)) { return obj.clone(); }
clone = _.isArray(obj) ? obj.slice() : _.extend({}, obj);
if (depth !== undefined && depth > 0) {
for (key in clone) {
clone[key] = deepClone(clone[key], depth-1);
}
}
return clone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment