对象浅/深复制

/**测试数据*/
var parent = {
	numbers: [1, 2, 3],
	letters: ['a', 'b', 'c'],
	obj: {
		prop: 1
	},
	bool: true,
	fn: function(p){
			alert(p);
	}
};

/**浅复制*/
function copy(p,c){
	var c = c || {}; 
	for(var i in p){
		c[i] = p[i];
	}
	return c;
}

/**深度复制*/
function deepCopy(p, c) {
    var c = c || {};
    for (var i in p) {
	    if (typeof p[i] === 'object') {
	        c[i] = (p[i].constructor === Array)?[]: {};
	        deepCopy(p[i], c[i]);
	    } else {
	        c[i] = p[i];
	    }
    }
    return c;
}


你可能感兴趣的:(C++,c,C#)