對於引用類型變量,棧內存中存放的是該對象的訪問地址,在堆內存中爲該值分配空間,因爲這種值的大小不固定,所以不能把他們保存到棧內存中;但內存地址大小是固定的,所以能夠將堆內存地址保存到棧內存中。這樣,當查詢引用類型的變量時,就先從棧中讀取堆內存地址,而後再根據該地址取出對應的值。javascript
引用類型:從一個變量向另外一個新變量複製引用類型的值,其實複製的是指針,最終兩個變量最終都指向同一個對象,會影響到原數據java
let arr1 = [1, {'name': 'a'}] let shallowClone = (arr) => { let newArr = [] for (let prop in arr) { if (arr.hasOwnProperty(prop)) { newArr[prop] = arr[prop] } } return newArr } let arr2 = shallowClone(arr1) arr2[0] = 2 arr2[1].name = 'b' console.log(JSON.stringify({arr1})) // {"arr1":[1,{"name":"b"}]} console.log(JSON.stringify({arr2})) // {"arr2":[2,{"name":"b"}]} // 基本數據類型未受到影響、引用數據類型仍是受到影響了 複製代碼
let arr1 = [1, {'name': 'a'}] let arr2 = arr1.concat() arr2[0] = 2 arr2[1].name = 'b' console.log(JSON.stringify({arr1})) // {"arr1":[1,{"name":"b"}]} console.log(JSON.stringify({arr2})) // {"arr1":[2,{"name":"b"}]} 複製代碼
let arr1 = [1, {'name': 'a'}] let arr2 = arr1.slice() arr2[0] = 2 arr2[1].name = 'b' console.log(JSON.stringify({arr1})) // {"arr1":[1,{"name":"b"}]} console.log(JSON.stringify({arr2})) // {"arr2":[2,{"name":"b"}]} 複製代碼
let obj1 = { 'name': 'k', 'msg': { 'age': '10', 'sex': '男' }, } let obj2 = Object.assign({}, obj1) obj2.name = 'kk' obj2.msg.age = '20' console.log(JSON.stringify({obj1})) // {"obj1":{"name":"k","msg":{"age":"20","sex":"男"}}} console.log(JSON.stringify({obj2})) // {"obj2":{"name":"kk","msg":{"age":"20","sex":"男"}}} 複製代碼
let arr1 = [1, 2, 3, {'name': 'kk'}] let arr2 = [...arr1] arr2.push(4); arr2[3].name = 'kkk' console.log(JSON.stringify({arr1})) // {"arr1":[1,2,3,{"name":"kkk"}]} console.log(JSON.stringify({arr2})) // {"arr2":[1,2,3,{"name":"kkk"},4]} 複製代碼
let arr1 = [1, {'name': 'a'}] // 判斷數據類型 let checkType = (data) => { return Object.prototype.toString.call(data).slice(8, -1) } let deepClone = (data) => { let newData, targetType = checkType(data) if (targetType === 'Object') { newData = {} } else if (targetType === 'Array') { newData = [] } else { return data } // 遍歷 for (let i in data) { let val = data[i] if (checkType(val) === 'Object' || checkType(val) === 'Array') { newData[i] = deepClone(val) } else { newData[i] = val } } return newData } let arr2 = deepClone(arr1) arr2[0] = 2 arr2[1].name = 'b' console.log(JSON.stringify({arr1})) // {"arr1":[1,{"name":"a"}]} console.log(JSON.stringify({arr2})) // {"arr2":[2,{"name":"b"}]} 複製代碼
該方法存在侷限性markdown