Is there a better approach for implementing getter/setter pattern with array properties?
For instance:
export class User {
private _name: string;
set name(value: string) {
this._name = value;
}
get name(): string {
return this._name;
}
private _roles = new Array<string>();
set roles(value: Array<string>) {
this._roles = value;
}
get roles(): Array<string> {
return this._roles;
}
constructor() {
}
}
Changing user.name triggers the setter method, but modifying items in roles does not.
I believe the reason it doesn't trigger the setter is because adding items to the array doesn't change the pointer, but simply appends to the existing space (please correct me if I'm mistaken).
How can we achieve the desired behavior with getter/setter on array properties?