js 深拷贝
var arr = [1,2,3,4,5] var arr2 = copyArr(arr) function copyArr(arr) { let res = [] for (let i = 0; i < arr.length; i++) { res.push(arr[i]) } return res }
var arr = [1,2,3] var arr2 = arr.slice(0) arr[2] = 5
var arr = [1,2,3] var arr2 = arr.concat() arr[2] = 5
ES 6 var arr = [1,2,3,4,5] var [ ...arr2 ] = arr arr[2] = 5
转换成json再转换成对象实现对象的深拷贝
var obj = { name: 'F', sex: 'm', old: '11' } var obj2 = JSON.parse(JSON.stringify(obj))