jeudi 11 juillet 2019

How to know if an instance of class came from a Singleton?

I have to evaluate if an instance of a class came from a Singleton or no?

I know that in JavaScript to create a Singleton I need to save in the constructor a variable instance (the name could be whatever) that save this.

class Singleton {
  constructor() {
    const instance = this.constructor.instance;
    if (instance) return instance;
    this.constructor.instance = this;
  }
}

But if I don't have that code and I just get back the instance of the class, for example:

let singletonOrMaybeNo = new Singleton()

And I just have singletonOrMaybeNo;

How can I know if that instance is a Singleton or No?

Thanks!!

Aucun commentaire:

Enregistrer un commentaire