# JavaScript singleton with methods and properties

I am diving deeper into JavaScript and starting to learn more about prototypes and simulating classes etc. I am used to working with the Module pattern and would like to try and combine this pattern along with working with prototyping into an own little framework (purely for learning purposes).

I have devised a little construct and I was hoping if some of you advanced users could give me some feedback on the particular construct.

What I am trying to achieve is to create a singleton with methods and properties one could access, I would like to extend/build this via the prototype as speed is something I care a lot about. The thing I still have doubts about is whether or not this would be a suitable construct for what I have in mind, whether or not this would be fast etc.

Much appreciated! Here's the code.

var Mui = (function (window, document, undefined) {

Mui = function () {
this.Version = {
Major  : '0',
Minor  : '1',
Bugfix : '0'
};
};

Mui.prototype = {
sayHi : function () {
}
};

return new Mui;

}(window, this.document));

• So, you want the Version property to be hard-coded into the Mui constructor, yes? – Šime Vidas Feb 24 '12 at 21:16
• @Šime Vidas Not really, to be honest i added it so the constructor wouldn't be empty. I would assume i want to update the numbers more dynamicly. – ngr Feb 24 '12 at 21:21

A singleton is an object which is the only instance of its type. Therefore, it makes most sense to define all non-function properties and methods directly on that singleton:

var singleton = {
prop1: ...,
prop2: ...,
method1: function () { ... },
method2: function () { ... }
};


Extending the singleton is easy:

singleton.prop3 = ...;
singleton.method3 = ...;


If you want you constructor function to be a singleton, you are going to have to make a few tweaks to your code:

var Mui = function () {
//the cached instance
var instance;

//rewrite the constructor
Mui = function Mui() {
return instance;
};

//carry over the prototype properties
Mui.prototype = this;

//the instance
instance = new Mui();

//reset the constructor pointer
instance.constructor = Mui;

//all the functionality
instance.version = {
Major  : '0',
Minor  : '1',
Bugfix : '0'
};
return instance;
};

Mui.prototype.sayHi = function() {
};

var mui_1 = new Mui();
var mui_2 = new Mui();


You can see this working here: http://jsfiddle.net/34vCH/3/

• He returns new Mui from his closure; you can't get at the Mui constructor without removing the closure as you've done here. It was a valid singleton object. There's really no reason for the constructor to be a singleton; usually "singleton" in prototype-oo-speak refers to an object rather than a function. – Dagg Feb 24 '12 at 22:16
• @GGG, you can do this with a closure but I don't care for the implementation. I agree that 99% of the time you should just use an object literal for a singleton. Because that was already an answer and the questions involved a constructor function I decided to show an alternative solution. – Paul Feb 24 '12 at 23:07

I agree with Šime Vidas; simplicity is best here.

var mui = {

version: {
major  : '0',
minor  : '1',
bugfix : '0'
},

sayHi : function () {

On a side note, don't use this to refer to the global object. document would work just as well as this.document.