DEV Community

Alexandre for SNOWCODES

Posted on • Edited on

5 1

Singleton, return to javascript basics

Simple example of singleton

var MySingleton = (function MySingleton() {
 return {
   // YOUR PUBLIC CODE
   myFunction: function() {},
   myVariable: 2020
 };
})();

MySingleton.myVariable; // Outputs: 2020
MySingleton.myFunction(); // Outputs: void
Enter fullscreen mode Exit fullscreen mode

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) { });
Enter fullscreen mode Exit fullscreen mode

Simple ;)

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay