标签:style blog color io for ar cti div
至于为什么要去实现对象的深度克隆,这里就不说了,直接说实现,也是看别人代码总结的:
1 function cloneObject( obj ) { 2 var str = ‘‘; 3 var newObj = obj.constructor === Array ? [] : {}; 4 5 if ( typeof obj !== ‘object‘) { 6 return ; 7 } else if( window.JSON) { 8 str = JSON.stringify(obj); 9 newObj = JSON.parse(str); 10 } else { 11 for ( var i in obj ) { 12 newObj[i] = typeof obj[i] === ‘ object ‘ ? arguments.callee.call( obj[i] ) : obj[i]; 13 } 14 } 15 return newObj; 16 } 17 18 //for test 19 var p = [1, ‘2‘, [0, 1, 2], {a: 0, b: 1, c: 2}]; 20 //clone 21 var np = cloneObject(p); 22 console.log(obj, arr, newobj, newarr, np);
标签:style blog color io for ar cti div
原文地址:http://www.cnblogs.com/guoyongfeng/p/3908071.html