Last active
August 29, 2015 14:02
-
-
Save juliankrispel/68834bf3446e82f13f76 to your computer and use it in GitHub Desktop.
a little deep clone function
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
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