How to use pure javascript to achieve shallow copy and deep copy of an object instead of using es6

504 Gateway Time-out

504 Gateway Time-out


nginx
Mar.28,2021

shallow copy:

function shalowCopy(obj) {
    return {...obj}
}

Deep copy

function deepCopy(obj) {
    return JSON.parse(JSON.stringify(obj))

}


< H2 > update < / H2 >

you can take a look at this article

.

shallow copy: Object.assign {.obj}
Deep copy: copy layer by layer, pushState , JSON.parse+JSON.stringify


which way to achieve deep copy is efficient?

Menu