I am evaluating a way of use the Singleton Pattern called Monostate in JavaScript.
I have some code like the following:
class Boss {
get name() { return Boss._name }
set name(value) {
Boss._name = value;
}
get age() { return Boss._age }
set age(value) {
Boss._age = value
}
toString() {
return `
Boss' name is ${this.name}
and he is ${this.age} years old.
`
}
}
Boss._age = undefined;
Boss._name = undefined;
But I am not deeply understanding the difference between the Class scope vs the Instance scope
I mean if I make the following:
let boss = new Boss();
boss.name = 'Bob';
boss.age = 55;
let boss2 = new Boss();
boss2.name = 'Tom';
boss2.age = 33;
console.log(boss.toString());
console.log(boss2.toString());
I will always get the data of the second instance with name Tom
, but why?
Boss' name is Tom and he is 33 years old.