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
No description provided.
The text was updated successfully, but these errors were encountered:
首先要明白,JS里面,深度复制没有一个完美答案。
当一个数据可JSON,他的深度复制才有意义。
// 1.JSON的方法 // JSON的方法对,undefined,循环引用,正则,函数,都不行 const obj = { arr: [111, 222], obj: { key: '对象' }, a: () => { console.log('函数') }, date: new Date(), reg: /正则/ig, testValue: undefined, testValue: null, } const shadowCopyObj = JSON.parse(JSON.stringify(obj)) console.log(shadowCopyObj) // output // { arr: [ 111, 222 ], // obj: { key: '对象' }, // date: '2020-04-20T08:03:01.178Z', // reg: {}, // testValue: null }
//2. for ...in 递归实现 // 无法解决 函数,正则,日期,循环引用 const obj = { arr: [111, 222], obj: { key: '对象' }, a: () => { console.log('函数') }, date: new Date(), reg: /正则/ig, testValue: undefined, testValue: null, } function isObj(obj) { return (typeof obj === 'object' || typeof obj === 'function') && obj !== null } function deepCopy(obj) { let tempObj = Array.isArray(obj) ? [] : {} for (let key in obj) { tempObj[key] = isObj(obj[key]) ? deepCopy(obj[key]) : obj[key] } return tempObj } const deepCopyObj = deepCopy(obj) console.log(deepCopy(obj)) // output" // { arr: [ 111, 222 ], // obj: { key: '对象' }, // a: {}, // date: {}, // reg: {}, // testValue: null } // 3. 结构性复制,可惜是异步的 //https://developer.mozilla.org/zh-CN/docs/Web/Guide/API/DOM/The_structured_clone_algorithm#%E7%9B%B8%E5%85%B3%E9%93%BE%E6%8E%A5 // 没有完整的深拷贝方案。这是一个从上层不被支持的功能。
Sorry, something went wrong.
Mark24Code
No branches or pull requests
No description provided.
The text was updated successfully, but these errors were encountered: