索引es6
一、對象深拷貝數組
二、網絡圖片轉base64, 在線圖片點擊下載網絡
三、對象深拷貝函數
四、對象深拷貝測試
五、對象深拷貝日誌
六、對象深拷貝code
一、對象的深拷貝(一級屬性拷貝和多級屬性嵌套拷貝)對象
//深拷貝函數(知足屬性多級嵌套) function deepCloneObj(sourceObj){ var obj = (sourceObj instanceof Array) ? [] : {}; for (var key in sourceObj){ var tmp = sourceObj[key]; //處理重複引用,防止死循環 if (tmp === sourceObj){ continue; } if (typeof tmp === 'object'){ obj[key] = arguments.callee(tmp); //屬性爲對象,遞歸深度複製 } else{ obj[key] = tmp; } } return obj; } //
一、測試對象的深拷貝遞歸
var obj = {info:{name:'大錘'}, sign: 'xxx'}; var obj2 = Object.assign({}, obj); //一級屬性深拷貝 (es6的Object.assign函數特性) var obj3 = {...obj}; //一級屬性深拷貝 (es6擴展運算符) var obj4 = deepCloneObj(obj); //遞歸深拷貝全部層級屬性 var obj5 = JSON.parse(JSON.stringify(obj)); //深拷貝全部層級屬性 console.log('obj: ', JSON.stringify(obj)); console.log('obj2: ', JSON.stringify(obj2)); console.log('obj3: ', JSON.stringify(obj3)); console.log('obj4: ', JSON.stringify(obj4)); console.log('obj5: ', JSON.stringify(obj5)); console.log(".........end.........."); setTimeout(function(){ obj.sign = 'a**b'; obj.info.name = '一二三'; console.log('\n****update info ****'); console.log('obj: ', JSON.stringify(obj)); console.log('obj2: ', JSON.stringify(obj2)); console.log('obj3: ', JSON.stringify(obj3)); console.log('obj4: ', JSON.stringify(obj4)); console.log('obj5: ', JSON.stringify(obj5)); console.log(".........end.........."); },100); /* 打印日誌結果: obj: {"info":{"name":"大錘"},"sign":"xxx"} obj2: {"info":{"name":"大錘"},"sign":"xxx"} obj3: {"info":{"name":"大錘"},"sign":"xxx"} obj4: {"info":{"name":"大錘"},"sign":"xxx"} obj5: {"info":{"name":"大錘"},"sign":"xxx"} .........end.......... ****update info **** obj: {"info":{"name":"一二三"},"sign":"a**b"} //只拷貝了第一級屬性 obj2: {"info":{"name":"一二三"},"sign":"xxx"} //只拷貝了第一級屬性 obj3: {"info":{"name":"一二三"},"sign":"xxx"} //只拷貝了第一級屬性 obj4: {"info":{"name":"大錘"},"sign":"xxx"} //全部層級深拷貝 obj5: {"info":{"name":"大錘"},"sign":"xxx"} //全部層級深拷貝 */
//二、測試數組的深拷貝索引
setTimeout(function(){ console.log('\n\n.....數組的深拷貝測試....'); var arr = [{name: '小明'}, true, 18]; var arr2 = Object.assign([], arr); var arr3 = [...arr2]; var arr4 = deepCloneObj(arr); var arr5 = JSON.parse(JSON.stringify(arr)); console.log('arr: ', JSON.stringify(arr)); console.log('arr2: ', JSON.stringify(arr2)); console.log('arr3: ', JSON.stringify(arr3)); console.log('arr4: ', JSON.stringify(arr4)); console.log('arr5: ', JSON.stringify(arr5)); console.log("......end......\n"); setTimeout(function(){ arr[0].name = '五六七', arr[1] = false, arr[2] = 99; console.log('arr: ', JSON.stringify(arr)); console.log('arr2: ', JSON.stringify(arr2)); console.log('arr3: ', JSON.stringify(arr3)); console.log('arr4: ', JSON.stringify(arr4)); console.log('arr5: ', JSON.stringify(arr5)); }, 100); /* 數組測試打印日誌結果: .....數組的深拷貝測試.... arr: [{"name":"小明"},true,18] arr2: [{"name":"小明"},true,18] arr3: [{"name":"小明"},true,18] arr4: [{"name":"小明"},true,18] arr5: [{"name":"小明"},true,18] ......end...... arr: [{"name":"五六七"},false,99] arr2: [{"name":"五六七"},true,18] //只拷貝了第一級屬性 arr3: [{"name":"五六七"},true,18] //只拷貝了第一級屬性 arr4: [{"name":"小明"},true,18] //全部層級深拷貝 arr5: [{"name":"小明"},true,18] //全部層級深拷貝 */ },1000);</pre>