Created
June 26, 2022 07:31
-
-
Save fronterior/8462e29de74f62719071154d0efe9fde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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