1 //isActiveClone防止循环引用
 2 function clone(obj) {
 3     if (obj === null || typeof obj !== 'object' || 'isActiveClone' in obj) {
 4         return obj;
 5     }
 6 
 7     var temp = obj.constructor(); //使temp的prototype与obj的相同
 8 
 9     for (var key in obj) {
10         if (Object.prototype.hasOwnProperty.call(obj, key)) {
11             obj['isActiveClone'] = null;
12             temp[key] = clone(obj[key]);
13             delete obj['isActiveClone'];
14         }
15     }
16     return temp;
17 }

 

posted on 2015-07-21 22:07  biganqiang  阅读(146)  评论(0编辑  收藏  举报