JS: Does Object.assign() create deep copy or shallow copy

后端 未结 7 1116
粉色の甜心
粉色の甜心 2020-11-30 03:21

I just came across this concept of

var copy = Object.assign({}, originalObject);

which creates a copy of original object into the \"

7条回答
  •  醉话见心
    2020-11-30 04:06

    For small Data structures I see that JSON.stringify() and JSON.parse() work nice.

    // store as JSON
    var copyOfWindowLocation = JSON.stringify(window.location)
    console.log("JSON structure - copy:", copyOfWindowLocation)
    // convert back to Javascript Object
    copyOfWindowLocation = JSON.parse(copyOfWindowLocation)
    console.log("Javascript structure - copy:", copyOfWindowLocation)
    

提交回复
热议问题