Skip to content

Instantly share code, notes, and snippets.

@fronterior
Created June 26, 2022 07:31
Show Gist options
  • Save fronterior/8462e29de74f62719071154d0efe9fde to your computer and use it in GitHub Desktop.
Save fronterior/8462e29de74f62719071154d0efe9fde to your computer and use it in GitHub Desktop.
const AbstractSingleton = (() => {
const constructors = new Set();
return class AbstractSingleton {
constructor() {
if (constructors.has(this.constructor)) throw new Error(`Already created instance: ${this.constructor.name}`);
constructors.add(this.constructor);
}
};
})();
class A extends AbstractSingleton {}
new A();
new A(); // Uncaught Error: Already created instance: A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment