cloneObj 深拷贝
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 cloneObj = function(obj){ | |
var str, newobj = obj.constructor === Array ? [] : {}; | |
if(typeof obj !== 'object'){ | |
return; | |
} else if(window.JSON){ | |
str = JSON.stringify(obj), //系列化对象 | |
newobj = JSON.parse(str); //还原 | |
} else { | |
for(var i in obj){ | |
newobj[i] = typeof obj[i] === 'object' ? | |
cloneObj(obj[i]) : obj[i]; | |
} | |
} | |
return newobj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment