Skip to content
New issue

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

13.deepClone #13

Open
mewcoder opened this issue Mar 14, 2024 · 1 comment
Open

13.deepClone #13

mewcoder opened this issue Mar 14, 2024 · 1 comment

Comments

@mewcoder
Copy link
Owner

实现深拷贝

@mewcoder
Copy link
Owner Author

mewcoder commented Mar 14, 2024

function deepClone(source, map = new WeakMap()) {

    if (source === null || typeof source !== "object") return source // 基本类型返回
    if (map.get(source)) return map.get(source);

    const target = Array.isArray(source) ? [] : {}

    map.set(source, target);

    for (let key in source) {
        if (source.hasOwnProperty(key)) {
            target[key] = deepClone(source[key], map);
        }
    }
    return target;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant