Singleton, return to javascript basics
Alexandre
Posted on December 19, 2019
Simple example of singleton
var MySingleton = (function MySingleton() {
return {
// YOUR PUBLIC CODE
myFunction: function() {},
myVariable: 2020
};
})();
MySingleton.myVariable; // Outputs: 2020
MySingleton.myFunction(); // Outputs: void
Combine reactive code and singleton with Rxjs
var MySingleton = (function MySingleton(rxjs) {
var user$ = new rxjs.BehaviorSubject(null);
return {
user$
};
})(rxjs);
MySingleton.user$.subscribe(function(user) { });
Simple ;)
💖 💪 🙅 🚩
Alexandre
Posted on December 19, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript Why Does jQuery or a DOM Method Like `getElementById` Fail to Find an Element?
November 22, 2024