class Singleton {
test;
constructor() {
if(typeof Singleton.instance === 'object') { //if object instance already exists
return Singleton.instance; //return object reference
}
Singleton.instance = this; //else store object instance
return this; //and return this
}
get test() {
return this.test;
}
set test(value) {
this.test = value;
}
}
var obj1 = new Singleton();
var obj2 = new Singleton();
obj1.test = 1;
obj2.test;
console.log(obj1 === obj2);
console.log(obj2.test);
class State {
private static instance: State;
private _token: string;
constructor() {
this._token = '';
if (typeof State.instance === 'object') {
return State.instance;
}
State.instance = this;
return this;
}
get token() {
return this._token;
}
set token(value: string) {
this._token = value;
}
set refreshToken(value: string) {
this._refreshToken = value;
}
set userId(value: string) {
this._userId = value;
}
set name(value: string) {
this._name = value;
}
set message(value: string) {
this._message = value;
}
toJSON() {
return {
token: this.token,
};
}
}