Skip to content

Instantly share code, notes, and snippets.

@juliankrispel
Last active August 29, 2015 14:02
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 juliankrispel/68834bf3446e82f13f76 to your computer and use it in GitHub Desktop.
Save juliankrispel/68834bf3446e82f13f76 to your computer and use it in GitHub Desktop.
a little deep clone function
var clone = function(thing){
switch(is(thing)){
case 'object':
var newobj = {};
for (var key in thing){
newobj[key] = clone(thing[key]);
}
return newobj;
case 'array':
var newarr = {};
for (var i in thing){
newarr[i] = clone(thing[i]);
}
return newarr;
default:
return thing;
}
};
var is = function(variable, typeName){
var type = Object.prototype.toString.call( variable );
if(typeName === undefined){
return type;
}else if(is(typeName) == 'string'){
return type.substr('[object '.length, type.length - 1).toLowerCase() === typeName.toLowerCase();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment