singleton in javascript

Solutions on MaxInterview for singleton in javascript by the best coders in the world

showing results for - "singleton in javascript"
Louane
29 Jan 2018
1var SingletonFactory = (function(){
2    function SingletonClass() {
3        //do stuff
4    }
5    var instance;
6    return {
7        getInstance: function(){
8            if (instance == null) {
9                instance = new SingletonClass();
10                // Hide the constructor so the returned object can't be new'd...
11                instance.constructor = null;
12            }
13            return instance;
14        }
15   };
16})();
17
Anna
22 Nov 2018
1I think the easiest way is to declare a simple object literal:
2
3var myInstance = {
4  method1: function () {
5    // ...
6  },
7  method2: function () {
8    // ...
9  }
10};
11If you want private members on your singleton instance, you can do something like this:
12
13var myInstance = (function() {
14  var privateVar = '';
15
16  function privateMethod () {
17    // ...
18  }
19
20  return { // public interface
21    publicMethod1: function () {
22      // All private members are accessible here
23    },
24    publicMethod2: function () {
25    }
26  };
27})();
28This has been called the module pattern, and it basically allows you to encapsulate private members on an object, by taking advantage of the use of closures.
29
30If you want to prevent the modification of the singleton object, you can freeze it, using the ES5 Object.freeze method.
31
32That will make the object immutable, preventing any modification to the its structure and values.
33
34If you are using ES6, you can represent a singleton using ES Modules very easily, and you can even hold private state by declaring variables at the module scope:
35
36// my-singleton.js
37const somePrivateState = []
38
39function privateFn () {
40  // ...
41}
42
43export default {
44  method1() {
45    // ...
46  },
47  method2() {
48    // ...
49  }
50}
51Then you can simply import the singleton object to use it:
52
53import myInstance from './my-singleton.js'
54// ...
queries leading to this page
what is singleton javascript 3fcreate singleton object in javascriptsingleton javascriptjs singleton examplesingleton javascript examplesingleton class in javascriptwhat is singleton and contractor in javascriptsingleton class jscreating a singleton class in javascriptcreate singleton class in javascriptnodejs singletonsingleton in programming javascriptjavascript singleton patternsingleton javacsriptsingleton function javascriptsingleton pattern javscriptcreate singleton function in javascriptsingleton pattern example in jssingleton pattern javascript create a singleton class in javascriptjavascript define singleton patternjavascript singleton examplesingleton pattern jsjavascript singleton pattern examplesingleton in javascript functionsingleton in javascriptsingleton jshow to create and access a singleton in javascriptjs singletonwhere can we use singleton pattern in javascriptmeaning of singleton in javascriptdoes javascript support singletonsingleton class in jscreate a singleton javascripthow we can create singleton object in javascripthow to make a singleton class jssingleton method jsjavascript singletonsingleton in jsjs singleton classjavascript singleton applications javascript singleton classimplement singleton pattern in javascriptjs create singletonsingleton pattern in jswhat is singleton in javascriptsingletons in jsjavascript singleton 3fimplement singleton javascriptjavascript class singleton examplesingleton pattern in javascriptcreate singleton in javascriptsingleton object javascriptsingleton class javascripthow to create singleton class in jssingleton javascript functionjavascript singleton examples create a singleton in javascriptsingleton js functionsingleton object in javascriptsingleton using class in jssingleton in nodejshow to implement singleton class in javascriptsingleton function in javascripthow to create a singleton class in javascriptsingleton used in jssingleton in javascript