Created
August 3, 2013 11:37
-
-
Save sTiLL-iLL/6146159 to your computer and use it in GitHub Desktop.
kustom klone method
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
function clone(src) { | |
function mixem(src, dst, fnc) { | |
var i, em, nm, j = {}; | |
for(nm in src){ | |
j = src[nm]; | |
if(!(nm in dst) || (dst[nm] !== j && (!(nm in em) || em[nm] !== j))){ | |
dst[nm] = fnc ? fnc(s) : j; | |
} | |
} | |
return dst; | |
} | |
// this handles any: non-object, or function | |
if(!src || typeof src != "object" || Object.prototype.toString.call(src) === "[object Function]"){ | |
return src; | |
} | |
if(src.nodeType && "cloneNode" in src){ | |
return src.cloneNode(true); | |
} | |
if(src instanceof Date){ | |
return new Date(src.getTime()); | |
} | |
if(src instanceof RegExp){ | |
return new RegExp(src); | |
} | |
var r, i, l; | |
if(src instanceof Array){ | |
r = []; | |
for(i = 0, l = src.length; i < l; ++i){ | |
if(i in src){ | |
r.push(clone(src[i])); | |
} | |
} | |
// don't clone functions... performance | |
// }else if(d.isFunction(src)){ | |
// // function | |
// r = function(){ return src.apply(this, arguments); }; | |
} | |
else{ | |
r = src.constructor ? new src.constructor() : {}; | |
} | |
return mixem(r, src, clone); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment