标签:
1 //代码读书记录,请勿转载。 产权非本人所有。
var util={ 2 getType:function(o){ //判断对象类型 3 var _t; 4 return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase(); 5 }, 6 deepClone:function(source){ //深拷贝 7 var destination=this.getType(source); 8 destination=destination===‘array‘?[]:(destination===‘object‘?{}:source); 9 for (var p in source) { 10 if (this.getType(source[p]) === "array" || this.getType(source[p]) === "object") { 11 destination[p] = this.getType(source[p]) === "array" ? [] : {}; 12 destination[p]=arguments.callee(source[p]); 13 } else { 14 destination[p] = source[p]; 15 } 16 } 17 return destination; 18 } 19 }; 20 21 22 var obj1={attr:100}; 23 var obj2=util.deepClone(obj1); //将obj1深拷贝到obj2 24 obj2.attr=200; //修改obj2的属性值 25 console.log(obj1.attr); //obj1属性值未发生变化
标签:
原文地址:http://www.cnblogs.com/shidengyun/p/5068951.html