class MyClass {
constructor() {
this.points = [];
// I need to call this after the components are mounted
// So keeping the setup separate
this.setupMyClass();
}
setupMyClass() {
let {points} = this;
points = [...points, {x: 20, y:20}];
// ugly code
// need to repeat 'this.' everytime I use the variable
// this.points = [...this.points, {x: 20, y: 20}];
console.log('points', points);
console.log('this.points', this.points);
}
}
myClassInstance = new MyClass();
JSFiddle here
Output:
points: [{..}]
this.points: []
I thought arrays were sent by reference while other values are copied by value. This answer supports the same. What's happening here?
I need to access variables of MyClass in a neat way, how do I do it?