Created
February 4, 2020 05:16
-
-
Save Jordan-Gilliam/37b2e74df15f53d50e9c23b1bb8def93 to your computer and use it in GitHub Desktop.
Create a true singleton with es6 Proxies
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
| function makeSingleton(func) { | |
| let instance, | |
| return new Proxy(func, { | |
| construct: function (target, args) { | |
| if (!instance) { | |
| instance = new func(); | |
| } | |
| return instance; | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment