<!-- 分类 -->
基础类型 - Number String Boolean null undefined Symbol BigInt
引用类型 - Object
<!-- 存储位置 -->
基础类型 - 存储在栈内存中
引用类型 - 在栈内存中存储的为一串堆内存的地址
<!-- 区别 -->
数据结构堆比栈大,栈比堆快
基础数据占用内存比较小,引用类型大小是动态的
<!-- 拷贝 -->
1. 浅拷贝-直接赋值
o2 = o1
2. 浅拷贝-JSON转换拷贝
JSON.parse(JSON.stringify())
undefined function symbol Date RegExp Error等特殊类型有问题
3. 第一层深拷贝
...
Object.assign()
Array.concat()
Array.slice()
4. 深拷贝
const deepClone = (source) => {
// 如果不是对象直接返回
if (typeof source !== 'object') return source
// 遍历拷贝
let target = Array.isArray(source) ? [] : {}
for (let key in source) {
if (typeof source[key] === 'object') {
target[key] = deepClone(source[key])
} else {
target[key] = source[key]
}
}
// 返回深拷贝对象
return target
}
javascript — 2021年7月4日