[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();
相關文章
相關標籤/搜索