ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` ## (一)slice方法 let arr = [1,2,3,4] let arr1= arr.slice() //或者是   let arr1= arr.slice(0) arr[0] = 6 console.log(arr)  // [6, 2, 3, 4] console.log(arr1) ``// [1, 2, 3, 4] ``` ## (二)自己封装一个myClone函数 ~~~ Array.prototype.myClone = function(){ let newArr=[]; for(let i=0;i<this.length;i++) { newArr.push(this[i]); } return newArr; } let arr = ['aaa','bbb','ccc','wwwww','ddd'] let arr2 = arr.myClone() console.log(arr) //["aaa", "bbb", "ccc", "wwwww", "ddd"] console.log(arr2) //["aaa", "bbb", "ccc", "wwwww", "ddd"] console.log( arr2 === arr ) //false ~~~ ## (三)展开运算符 \[...arr\] ~~~ const arr1 = [1, 2]; const arr2 = [...a1]; arr1[0] = 6 console.log(arr1) // [6, 2] console.log(arr2) // [1, 2] ~~~ ## (四)concat方法 ~~~ var arr1 = [1,2,3] var arr2 = arr1.concat() //或者是 var arr2 = arr1.concat([]) arr1[0] = 6 console.log(arr1) //[6,1,2] console.log(arr2) //[1,2,3] ~~~ ## (五)Object.assign() ~~~ let arr = [1,2,3,4]; let arr1 = []; Object.assign(arr1,arr); arr[0] = 6; console.log(arr); // [6, 2, 3, 4] console.log(arr1); // [1, 2, 3, 4]    ~~~ (六) ~~~tsx const deepClone(obj)=>JSON.parse(JSON.stringify(obj)) ~~~ (七) ~~~tsx function type(obj){ var type=Object.prototype.toString.call(obj) var map={ '[object Boolean]' : 'boolean', '[object Number]' : 'number', '[object String]' : 'string', '[object Function]' : 'function', '[object Array]' : 'array', '[object Date]' : 'date', '[object RegExp]' : 'regExp', '[object Undefined]': 'undefined', '[object Null]' : 'null', '[object Object]' : 'object' } if(obj instanceof Element){ return 'element' } return map[type] } function deepClone(item){ if(!item)return item var types=['number','string','boolean'],result if(types.includes(type(item))){ result=item } if(result===undefined){ if(type(item)==='array'){ console.log('arr') result=[] item.forEach((child,index,array)=>{ result[index]=deepClone(child) }) }else if(type(item)==='object'){ if(item.nodeType&& typeof item.cloneNode=='function'){ retult=item.cloneNode(true) //真实的复制函数并没有走到这里 }else if(!item.prototype){ // 检查是否是可循环的 if(item instanceof Date){ result=new Date(item) }else{ result={} for(var i in item){ result[i]=deepClone(item[i]) } } }else{ console.log('发现了其他类型') } }else{ result=item } }else{ console.log('123',result) } return result } let arr=[1,2,3] var arr2=deepClone(arr) console.log(arr2,'arr2') arr.push(1) console.log(arr2,'arr2-after') ~~~