Fun Fact: with nested objects, the nested object property is copied by reference as a property to the new Object.
let user = { name: "John", sizes: { height: 182, width: 50 } }; let clone = Object.assign({}, user); console.log(Object.is(user,clone)); //false console.log(Object.is(user.sizes, clone.sizes)); //true
To avoid this, the easiest way in JavaScript to deep clone an object is
let clone2 = JSON.parse(JSON.stringify(user)); console.log(Object.is(user.sizes, clone2.sizes)); // false
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Fun Fact: with nested objects, the nested object property is copied by reference as a property to the new Object.
To avoid this, the easiest way in JavaScript to deep clone an object is