[javascript] 对象拷贝

 1 Object.prototype.clone = function() {
 2   var copy = (this instanceof Array) ? [] : {};
 3   for (attr in this) {
 4     if (!obj.hasOwnProperty(attr)) continue;
 5     copy[attr] = (typeof this[i] == "object")?obj[attr].clone():obj[attr];
 6   } 
 7   return copy;
 8 };
 9 
10 
11 a = {k1:1, k2:2, k3:3};
12 b = a.clone();

 

posted on 2013-12-11 16:00  bluefrog  阅读(240)  评论(0编辑  收藏  举报