stay hungry stay foolish

js深拷贝

var obj = {a:1};
var obj1 = {
	b:2,
	c:function(){
		return 3;
	},
	d:obj,
	e:[1,2,3],
	f:null,
	g:undefined
}
function copy_1(obj){
	var result = {};
    if (obj && typeof obj == 'object') {
        if (obj instanceof Array) {
            result = [];
        }
        for (var key in obj) {
            result[key] = copy_1(obj[key]);
        }
    } else {
        result = obj;
    }
    return result;
}
//使用JSON实现拷贝,将会忽略function和undefined
function copy_2(obj){
	return JSON.parse(JSON.stringify(obj));
}
console.log(copy_1(obj1));
console.log(copy_2(obj1));