I faced an issue recently:
I had an object Recipe
with a primitive datatype i.e string
.
export class Recipe {
public name: string;
}
When I created an object of this type and passed it around components, every component got a new copy of this object.
I google and found in one of the answers that its because my object contains a primitive datatype and hence it can't be passed with reference and the receiver will get a copy instead.
Fair enough.
The problem is, when I created an array of objects where the object contained primitive types, I was able to share the array with all its values successfully among other components. Any change in my array was reflecting in all places where I had passed it.
Why is that an object with primitive types can not be passed as a reference but an array can?