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
浅拷贝只拷贝一层,对复杂数据类型存在引用问题,所以需要层层拷贝即深拷贝。
JSON.parse(JSON.stringify(obj))
存在缺点:
var a = { a: undefined, b: function(){}, c: new Date(), d: new RegExp(), e: Symbol('a') } JSON.parse(JSON.stringify(a)) // 结果 //{ // c: "2019-08-04T14:14:57.946Z", // d: {} //}
function is(value, type){ return Object.prototype.toString.call(value) === `[object ${type}]` } function deepClone(obj, hash = new WeakMap()) { if (is(obj, 'RegExp')) return new RegExp(obj) if (is(obj, 'Date')) return new Date(obj) if (obj === null || typeof obj !== 'object') return obj if (hash.has(obj)) return hash.get(obj) var target = new obj.constructor() hash.set(obj, target) Reflect.ownKeys(obj).forEach(v => { if (is(obj[v], 'Object')) { target[v] = deepClone(obj[v], hash) } else { target[v] = obj[v] } }) return target } var a = { a: undefined, b: function(){}, c: new Date(), d: new RegExp(), e: Symbol('a'), f: {a: {b: 1}}, g: [[1], [2], [3]] } var b = deepClone(a)
【进阶4-3期】面试题之如何实现一个深拷贝 这儿有20道大厂面试题等你查收
The text was updated successfully, but these errors were encountered:
sihai00
No branches or pull requests
实现深拷贝
浅拷贝只拷贝一层,对复杂数据类型存在引用问题,所以需要层层拷贝即深拷贝。
JSON
存在缺点:
实现深拷贝
参考
【进阶4-3期】面试题之如何实现一个深拷贝
这儿有20道大厂面试题等你查收
The text was updated successfully, but these errors were encountered: