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
function deepClone(origin, hasMap = new WeakMap()) { if (typeof origin !== 'object' || origin === null ) { return origin; } if (origin instanceof Date) { return new Date(origin); } if (origin instanceof RegExp) { return new RegExp(origin); } // 解决这个对象重复拷贝问题 const hasKey = hasMap.get(origin); if (hasKey) { return hasKey; } let target = new origin.constructor(); hasMap.set(origin, target); for (let k in origin) { // origin.hasOwnProperty(k) if (Object.prototype.hasOwnProperty.call(origin, k)) { target[k] = deepClone(origin[k], hasMap); } } return target; }
The text was updated successfully, but these errors were encountered:
借用对象的构造函数,生成一个新的对象
const obj = {a: 1} console.log(new obj.constructor)
const origin = {}; let target = new origin.constructor(); target.a = 1; console.log(target); // {a: 1} console.log(origin); // {}
const origin = []; let target = new origin.constructor(); target[0] = 1; console.log(target); console.log(origin);
Sorry, something went wrong.
No branches or pull requests
The text was updated successfully, but these errors were encountered: