We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现浅拷贝的方法一般有2种, Object.assign 和 ...xx操作符
Object.assign
...xx
const obj = { name: 'chenying', age: 18, has: { vue: true } } const obj1 = {...obj} obj1.name = 'ying' console.log(obj) // { name: 'chenying', age: 18, has: { vue: true } } console.log(obj1) // { name: 'ying', age: 18, has: { vue: true } }
const obj = { name: 'chenying', age: 18, has: { vue: true } } const obj1 = Object.assign(obj) obj1.name = 'ying' console.log(obj) // { name: 'ying', age: 18, has: { vue: true } } console.log(obj1) // { name: 'ying', age: 18, has: { vue: true } }
最简单的深拷贝方法是使用JSON.parse(JSON.stringify(obj))
JSON.parse(JSON.stringify(obj))
const obj = { name: 'chenying', age: 18, has: { vue: true } } const obj1 = JSON.parse(JSON.stringify(obj)) obj1.has.vue = false console.log(obj) // { name: 'chenying', age: 18, has: { vue: true } } console.log(obj1) // { name: 'chenying', age: 18, has: { vue: false } }
但是这个方法有点缺陷,例如
所以一般我们会自己实现一个深拷贝函数
const obj = { name: 'chenying', age: 18, has: { vue: true } } function deepcopy(value) { if (value == null) { return value } if (value instanceof RegExp) { return new RegExp(value) } if (value instanceof Date) { return new Date(value) } if (typeof value !== 'object') { return value } let obj = new value.constructor() // 拿到[] 或者 {} for (let key in value) { obj[key] = deepcopy(value[key]) } return obj } let obj1 = deepcopy(obj) obj1.has.vue = false console.log(obj) // { name: 'chenying', age: 18, has: { vue: true } } console.log(obj1) //{ name: 'chenying', age: 18, has: { vue: false } }
但是这样写有一个缺陷,就是容易爆栈,例如我们这样调用
let obj2 = {} obj2.x = obj2 let obj3 = deepcopy(obj2) console.log(obj3)
那我们可以使用WeakMap的特性判断,如果之前这个值已经拷贝过了,就不需要在进行拷贝了
WeakMap
function deepcopy(value, hash = new WeakMap) { if (value == null) { return value } if (value instanceof RegExp) { return new RegExp(value) } if (value instanceof Date) { return new Date(value) } if (typeof value !== 'object') { return value } let obj = new value.constructor() // 拿到[] 或者 {} if (hash.get(value)) { // 如果hash表里面有这个值就不需要走拷贝了 return hash.get(value) } hash.set(value, obj) for (let key in value) { obj[key] = deepcopy(value[key], hash) } return obj }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
深拷贝和浅拷贝
浅拷贝
实现浅拷贝的方法一般有2种,
Object.assign
和...xx
操作符深拷贝
最简单的深拷贝方法是使用
JSON.parse(JSON.stringify(obj))
但是这个方法有点缺陷,例如
所以一般我们会自己实现一个深拷贝函数
但是这样写有一个缺陷,就是容易爆栈,例如我们这样调用
那我们可以使用
WeakMap
的特性判断,如果之前这个值已经拷贝过了,就不需要在进行拷贝了The text was updated successfully, but these errors were encountered: