0

I am learning Design Patterns in JavaScript and I was going through the Singleton design pattern. Here is the code:

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 provided.
        options = options || {};
        //set the name parameter
        this.name = 'SingletonTester';
        //set the value of pointX
        this.pointX = options.pointX || 6;
        //set the value of pointY
        this.pointY = options.pointY || 10;
    }
            // this is our instance holder
    var instance;

    // this is an emulation of static variables and methods
    var _static = {
        name: 'SingletonTester',
        // This is a 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
});
var singletonTest1 = SingletonTester.getInstance({
    pointX: 15
});
console.log(singletonTest.pointX); // outputs 5
console.log(singletonTest1.pointX); // outputs 5

I do not understand why the variable instance gets some value when singletonTest1 is initiated.

7
  • In JS, you don't need Singleton, just use a global var. Commented Jan 20, 2016 at 12:26
  • Second that ^, but if you still want a Singleton emulation, then Google for "javascript singleton enforcer". There are some Git projects which do that. Makes no proper sense anyway. JS is JS. Commented Jan 20, 2016 at 13:02
  • @DavinTryon This is already a singleton pattern, it works fine. My confusion was, how come instance variable had value when singletonTest1 was initiated. At least read the question before comment. Commented Jan 21, 2016 at 13:26
  • @Ingmars question is not about I want singleton pattern, it is about something else. I wanted to ask why instance variable was still alive when singletonTest1 was initiated. Commented Jan 21, 2016 at 13:30
  • @BharatSoni the reason this works is because SingletonTester is a global var. Commented Jan 21, 2016 at 15:14

1 Answer 1

1

When the module SingletonTester is created, it is also called:

var SingletonTester = (function () {
    // ... stuff in here
    var instance;
})(); // <--- here

That last line is a function application ();. After that application the SingletonTester module contains all of it's enclosed state.

Since instance is a property closed over by the SingletonTester closure, instance is alive for the entire existence of SingletonTester.

Side note: Singleton pattern is primarily about creating a thread-safe static instance for sharing across a process. Since JavaScript is single-threaded, this is obviously not a problem. You can instead keep things simple and just use a global var.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, now I get it, got confused. Of course its a closure. Thanks a lot Davin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.