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.
Singleton, just use a global var.instancevariable had value whensingletonTest1was initiated. At least read the question before comment.instancevariable was still alive whensingletonTest1was initiated.SingletonTesteris a global var.