The js singleton pattern details the example

  • 2020-03-29 23:56:27
  • OfStack

What is a singleton?

Singletons require one and only one instance of a class to provide a global access point. So it bypasses the normal controller so that it has only one instance for the user to use, and the user doesn't care how many instances there are, so it's the designer's responsibility

In JavaScript, Singletons serve as a shared resource namespace which isolate implementation code from the global namespace so as to provide a single point of access for functions.

In javascript, a singleton is treated as a global namespace, providing a point of access to the object.

Usage scenarios

In practice, the Singleton pattern is useful when exactly one object is needed to coordinate others across a system. 

Singletons are more suitable for an object to interact with other systems.


The singleton is similar to the group leader of a group. There is only one group leader at a time, and the group leader is responsible for assigning, assigning and coordinating the work of the group members.

Example 1: this is the simplest singleton that stores properties and methods in the form of key and value

var A = {





Instance 2: first create a reference to an instance, then determine if the instance exists, if it does not, then create it, if it exists, then return it, make sure there is only one.

var mySingleton = (function () {
//Instance stores a reference to a singleton Instance
var instance;
function init() {
// Singleton
//Private methods and variables
function privateMethod(){
    console.log( "I am private" );
var privateVariable = "Im also private";
return {
  //Common methods and variables
  publicMethod: function () {
    console.log( "The public can see me!" );
  publicProperty: "I am also public"
return {
//If the instance does not exist, create one
getInstance: function () {
  if ( !instance ) {
    instance = init();
  return instance;
var singleA = mySingleton;
var singleB = mySingleton;
console.log( singleA === singleB ); // true

Example 3:

var SingletonTester = (function () {
  // options: an object containing configuration options for the singleton
  // e.g var options = { name: "test", pointX: 5}; 
  function Singleton( options )  {
    // set options to the options supplied
    // or an empty object if none are provided
    options = options || {};
    // set some properties for our singleton = "SingletonTester";
    this.pointX = options.pointX || 6;
    this.pointY = options.pointY || ; 
  // our instance holder 
  var instance;
  // an emulation of static variables and methods
  var _static  = {  
    name:  "SingletonTester",
    // Method for getting an instance. It returns
    // a singleton instance of a singleton object
    getInstance:  function( options ) {   
      if( instance  ===  undefined )  {    
        instance = new Singleton( options );   
        return  instance;      
  return  _static;
var singletonTest  =  SingletonTester.getInstance({
  pointX:  5
// Log the output of pointX just to verify it is correct
// Outputs: 5
console.log( singletonTest.pointX ); 

Related articles: