Skip to content Skip to sidebar Skip to footer

JavaScript Objects As Property Of Object Reference

I want to understand how objects work in JS. const obj = { inner: { a: 'Hello' } }; const clone = { ...obj }; // obj === clone -> false // !!! BUT !!! // obj.inner === clone.i

Solution 1:

... spread syntax creates a shallow copy anything deeper then level one will still stays as reference to original object

const obj = { inner: { a: 'Hello' } };

const clone = { ...obj };


console.log(obj === clone)

console.log(obj.inner === clone.inner)

Solution 2:

Shallow Cloning If the item being spread into the target is an object, only a reference to that object will be copied. The spread operator will not recursively deep clone properties. In addition, only own, enumerable properties are copied.

This Post will help Link


Post a Comment for "JavaScript Objects As Property Of Object Reference"