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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:59:01+00:00 2026-06-13T19:59:01+00:00

I have some reading about the two ways of creating a singleton in javascript

  • 0

I have some reading about the two ways of creating a singleton in javascript – simple object literal way and another with closure technique if we want to use private variables.

I am looking to create a utility function, for example

Singleton(classname);

Whatever class – “constructor function” i pass in here as argument the Singleton method converts this class into a Singleton object, PLUS after calling the new Classname() if somebody again fires new classname() he/she gets some new Error ( "Already instantiated once, this is Singleton" );

Use case is as below –

function Circle() {this.name = "Circle";}
SingleTon(Circle);
var circle1 = new Circle(); // returns the circle instance
var circle2 = new Circle(); // throws Error "Already instantiated once, this is Singleton"

I am just trying to define the “Singleton” method here.

I have seen similar example where a getInstance method is used to get the instance like – Singleton.getInstance(Circle) etc. but I am looking for the particular question above where another programmer habitual to creating an instance in the “new” way tries to fire new Circle(); second time somewhere in his code and receives an error.

Creating a singleton this way is one problem but main problem is throwing the “Error” for which to my understanding the Circle constructor needs to be modified somewhere in the Singleton function, not sure how to accomplish this.

Is there any solution ?

Thanks in advance !!

  • 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-13T19:59:02+00:00Added an answer on June 13, 2026 at 7:59 pm
    function Singleton(param){
        var count = 0, old = window[param];
        window[param] = function(){
             if(++count <= 1) return old;
             else alert('NO WAY!'); 
        }
    }
    

    You would call it as:

    Singleton('Circle');
    

    Demo: http://jsfiddle.net/maniator/7ZFmE/

    Remember this only works if Circle or any other function class is in the global window namespace. Anything in any other namespace would need some more manipulation to make it work fully.

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

Sidebar

Related Questions

I've been doing some reading about two (relatively) new concepts in the Javascript language
I have tried to do some reading about it, but I failed to find
After reading some articles about XSS I have incorporated HTMLPurifier into my zend framework
I have been reading about Scala for a while and even wrote some small
I'm reading some documents about both Servlets and PHP. These document have the same
I have been reading about creating offline websites using Gears and using HTML 5.
I have a question about how to use Dispose() and destructors. Reading some articles
I have been reading about UnitOfWork pattern for last two days but didn't understand
I am reading Real World Haskell (great book) and I have some confusion about
So I've been reading on concurrency and have some questions on the way (

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.