8000 深拷贝 · Issue #48 · icodinglee/TIL · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
深拷贝 #48
Open
Open
@icodinglee

Description

@icodinglee

第一种方法

function deepClone(obj){
   return  JSON.parse(JSON.stringify(obj))
}

第二种方法

function deepClone(obj){
    if(typeof obj !== 'object') return obj;
    if(obj === null) return null;
    if(obj instanceof RegExp){
        return new RegExp(obj)
    }
    if(obj instanceof Date){
        return new Date(obj)
    }
    let newObj = new obj.constructor;
    for(let key in obj){
        newObj[key] = deepClone(obj[key])
    }
    return newObj
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0