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
题目:
请编写一个js对象深拷贝的函数,需要考虑循环引用的情况。
参考答案:
function deepCopy(obj) { // hash表,记录所有的对象的引用关系 let map = new WeakMap(); function dp(obj) { let result = null; let keys = Object.keys(obj); let key = null, temp = null, existobj = null; existobj = map.get(obj); // 如果这个对象已经被记录则直接返回 if (existobj) { return existobj; } result = {} map.set(obj, result); for (let i = 0; i < keys.length; i++) { key = keys[i]; temp = obj[key]; if (temp && typeof temp === 'object') { result[key] = dp(temp); }else { result[key] = temp; } } return result; } return dp(obj); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目:
请编写一个js对象深拷贝的函数,需要考虑循环引用的情况。
参考答案:
The text was updated successfully, but these errors were encountered: