现在位置: 首页  >  编程技术  >  JS
js克隆对象
0 1227

Object.prototype.clone = function clone() {
   var copy = (this instanceof Array) ? [] : {};
   for (attr in this) {
       if (!obj.hasOwnProperty(attr)) continue;
       copy[attr] = (typeof this[i] == "object")?obj[attr].clone():obj[attr];
   }
   return copy;
};


function clone(obj) {
   var copy = (obj instanceof Array) ? [] : {};
   for (attr in obj) {
       if (!obj.hasOwnProperty(attr)) continue;
       copy[attr] = (typeof obj[attr] == "object")?clone(obj[attr]):obj[attr];
   }
   return copy;
};


 评论
 站内搜索