We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
浅拷贝是创建一个新对象,这个对象有着原始对象属性值的一份精确拷贝。如果属性是基本类型,拷贝的就是基本类型的值,如果属性是引用类型,拷贝的就是内存地址 ,所以如果其中一个对象改变了这个地址,就会影响到另一个对象。
深拷贝是将一个对象从内存中完整的拷贝一份出来,从堆内存中开辟一个新的区域存放新对象,且修改新对象不会影响原对象。
function deepClone(source) { function clone(source, map){ if(source !== null && typeof source == 'object') { let result = Array.isArray(source) ? [] : {} //对数组的情况进行考虑 if(map.has(source)) { return map.get(source) } map.set(source, result) for(let key in source) { //实例和原型上的可枚举属性都可以访问 if(source.hasOwnProperty(key)) { //只对实例上可枚举的属性进行拷贝 result[key] = clone(source[key], map) } } return result }else { return source } } let map = new WeakMap() //解决循环引用的问题 let result = clone(source, map) map = null return result }
参考文章:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Uh oh!
There was an error while loading. Please reload this page.
浅拷贝与深拷贝
浅拷贝是创建一个新对象,这个对象有着原始对象属性值的一份精确拷贝。如果属性是基本类型,拷贝的就是基本类型的值,如果属性是引用类型,拷贝的就是内存地址 ,所以如果其中一个对象改变了这个地址,就会影响到另一个对象。
深拷贝是将一个对象从内存中完整的拷贝一份出来,从堆内存中开辟一个新的区域存放新对象,且修改新对象不会影响原对象。
实现一个深拷贝函数
参考文章:
The text was updated successfully, but these errors were encountered: