js-singleton
A singleton wrapper for creating single instances of functions.
This module can wrap an arbitrary number of functions in the same
Singleton meaning you can init everything you need and then just
use require('js-singleton');
in other modules and it will work just fine.
Examples
Simple example in multi mode
const Singleton = ; { thisfirstname = firstname; thislastname = lastname;} var person = Singleton;console; // Prints George Clooney var anotherPerson = Singleton;console; // Prints George Clooney var anotherPersonMaybe = Singleton;console; // Prints George Clooney
Simple example in single mode
{ thisd = d;} const SingletonSingle = ff 125;const Singleton = ; var i1 = SingletonSingle;var i2 = SingletonSingle;var i3 = Singleton; console; // Prints 125console; // Prints 125console; // Prints 125
Example with getInstance() by passing a string alias of the function via setName()
{ thisa = a; thisb = b;} var complex = Singleton;const SingletonSingle = Complex;Singleton;SingletonSingle; // Equivalent to previous line var anotherComplex = Singleton; var someOtherComplex = SingletonSingle; console; // Prints 1 2;console; // Prints 1 2;console; // Prints 1 2;
Example with creating multiple aliases for the same function via setName()
const Singleton = ;{ thisheight = height; thiswidth = width;} var door = Singleton;Singleton;Singleton; var anotherDoor = Singleton; var anotherDoorMaybe = Singleton; const SingletonSingle = "otherDoorAlias)";var aDoorAlso = SingletonSingle; console; // Prints 2.3 1.1console; // Prints 2.3 1.1console; // Prints 2.3 1.1console; // Prints 2.3 1.1
Real world example
/** * * This module is the server module (server.js) * * @author Some Author * */ { // Some beautiful code} var options = {}; // some options const Singleton = ;Singleton;// equivalentconst SingletonSingle = server options; Singleton;// equivalentSingletonSingle; // No need to module.export /* * * In another file do * */;const Singleton = ;var myServer = Singleton;// equivalentconst SingletonSingle = "myServer";var myServer = SingletonSingle;