Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:35:26+00:00 2026-06-10T21:35:26+00:00

Reading the definition of the singleton function I can see how it is a

  • 0

Reading the definition of the singleton function I can see how it is a singleton.

But the usage is what confuses me, don’t you have to call getInstance?

var mySingleton = (function () {

  // Instance stores a reference to the Singleton
  var instance;

  function init() {

    // Singleton

    // Private methods and variables
    function privateMethod(){
        console.log( "I am private" );
    }

    var privateVariable = "Im also private";

    return {

      // Public methods and variables
      publicMethod: function () {
        console.log( "The public can see me!" );
      },

      publicProperty: "I am also public"
    };

  };

  return {

    // Get the Singleton instance if one exists
    // or create one if it doesn't
    getInstance: function () {

      if ( !instance ) {
        instance = init();
      }

      return instance;
    }

  };

})();

// Usage:

var singleA = mySingleton;
var singleB = mySingleton;
console.log( singleA === singleB ); // true
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:35:28+00:00Added an answer on June 10, 2026 at 9:35 pm

    Jerry has what is probably supposed to be in your example as the last three lines, but I think you need more of an understanding of what is going on in this code to understand why both the example Jarry provided and your original code evaluate to true.

    Take for instance this line of code in your original example:

    var singleA = mySingleton;
    

    What you’re saying here is that singleA equals the reference to mySingleton, then when you assign mySingleton to singleB, you are just giving singleB the same exact reference to mySingleton (no code is called within mySingleton, you are just setting a reference). So you are comparing the same exact reference to itself: the reference to mySingleton.

    Here’s how Jarry’s code works, take the line:

    var singleA = mySingleton.getInstance();
    

    This causes the mySingleton object to actually call a function within it that creates a singleton object. On the first call of this function it creates a new object in the code, and on every subsequent call it will return that same object that was created (this is based on the code within init()).

    This means that when you use mySingleton.getInstance you are creating a new object and setting singleA as a reference to that object. Then when you call “mySingleton.getInstance()” for “var singleB = mySingleton.getInstance()” it is returning the reference for the same object that was created on the previous line and assigning it to singleB. So when you are all said and done, the comparison singleA === singleB is true because you are comparing the same exact reference to itself: a reference to the object created by mySingleton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading some source code, I have found next traits definition: namespace dds { template
i am kinda confused reading the definition between the two. Can they actually intersect
I have been reading Beginners guide for oracle DB. The definition for precision and
When I was reading the definition for Creational Design Patter in Wikipedia, have come
I have been reading around the definition of OOP and couldn't get why PHP
While reading a code I came across, the following definition and initialization of a
I've been reading O'Reilly book Dojo - The Definitive Guid but somethings are still
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
Reading this When a bean is a singleton, only one shared instance of the
While reading up on numpy, I encountered the function numpy.histogram() . What is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.