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 8795283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:21:58+00:00 2026-06-13T23:21:58+00:00

I use the following Singleton pattern in JavaScript: var exampleClass =(function(){ //private var a

  • 0

I use the following Singleton pattern in JavaScript:

  var exampleClass =(function(){ 
                        //private
                        var a ="test"; 
                        function PrivateMethod()
                        {
                            return a;
                        }
                        //public
                        return{  
                            Test: function() { 
                                   alert(PrivateMethod()); 
                                  } 
                        }
                      })(); 

As I read through StackOverflow I see a lot of other implementations of Singleton and I start to doubt if I couldn’t make mine better.
I hope someone can tell me what’s right or wrong about doing it this way.

  • 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-13T23:21:59+00:00Added an answer on June 13, 2026 at 11:21 pm

    It depends what you want to achieve as the different implementations will have different benefits and limitations.

    The simplest implementation is just an object literal:

    var singleton = {
        property: "foo",
        method: function() {
            alert('bar');
        }
    }
    

    The implementation you mention in the question allows public and private methods by encapsulating the methods in a closure and returning what should be exposed.

    Here is an alternative which similarly would allow public and private methods and is more extensible:

    function MySingletonClass() {
    
      if ( arguments.callee._singletonInstance )
        return arguments.callee._singletonInstance;
      arguments.callee._singletonInstance = this;
    
      this.Foo = function() {
        // ...
      }
    }
    
    var a = new MySingletonClass()
    var b = MySingletonClass()
    Print( a === b ); // prints: true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just use following code to add an image to my project, var paper
When i use following: var child:DisplayObjectContainer = dso.getChildAt( dso.numChildren -1 ) as DisplayObjectContainer; trace(child);
(I use the word var in the following because I'm not sure what it
I am trying to implement singleton pattern and i am getting the following error
I plan to use the following pattern to make use of the requireJS based
I would like to use singleton pattern in a multithreaded program. Double-checked locking method
I'm developing plugins in Eclipse which mandates the use of singleton pattern for the
I have decided to use the singleton pattern for my application. It makes the
The following concerns an iPhone app. I want to use a singleton to handle
In the Singleton pattern why is that we have to use a static object

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.