singleton in javascript

var SingletonFactory = (function(){
    function SingletonClass() {
        //do stuff
    }
    var instance;
    return {
        getInstance: function(){
            if (instance == null) {
                instance = new SingletonClass();
                // Hide the constructor so the returned object can't be new'd...
                instance.constructor = null;
            }
            return instance;
        }
   };
})();

3.86
7
Awgiedawgie 440220 points

                                    I think the easiest way is to declare a simple object literal:

var myInstance = {
  method1: function () {
    // ...
  },
  method2: function () {
    // ...
  }
};
If you want private members on your singleton instance, you can do something like this:

var myInstance = (function() {
  var privateVar = '';

  function privateMethod () {
    // ...
  }

  return { // public interface
    publicMethod1: function () {
      // All private members are accessible here
    },
    publicMethod2: function () {
    }
  };
})();
This 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.

If you want to prevent the modification of the singleton object, you can freeze it, using the ES5 Object.freeze method.

That will make the object immutable, preventing any modification to the its structure and values.

If 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:

// my-singleton.js
const somePrivateState = []

function privateFn () {
  // ...
}

export default {
  method1() {
    // ...
  },
  method2() {
    // ...
  }
}
Then you can simply import the singleton object to use it:

import myInstance from './my-singleton.js'
// ...

3.86 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
singleton function in javascript what is singleton in javascript how to create singleton class in js singleton javascript function how we can create singleton object in javascript how to create and access a singleton in javascript what is singleton and contractor in javascript implement singleton javascript singleton pattern in js how to create a singleton class in javascript singleton js function singleton object in javascript javascript define singleton pattern javascript singleton pattern example create a singleton javascript implement singleton pattern in javascript singleton javascript example singleton using class in js singleton pattern example in js singleton used in js singleton class js singletons in js singleton method js singleton class in js how to make a singleton class js javascript singleton examples javascript singleton applications singleton function javascript js create singleton create a singleton in javascript create a singleton class in javascript singleton in javascript function creating a singleton class in javascript singleton in js js singleton create singleton object in javascript javascript class singleton example javascript singleton example Singleton pattern js javascript singleton pattern javascript singleton? javascript singleton class create singleton in javascript singleton pattern javscript does javascript support singleton singleton js singleton class in javascript singleton javascript javascript singleton where can we use singleton pattern in javascript singleton pattern in javascript create singleton class in javascript singleton class javascript singleton pattern javascript singleton in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source