Skip to main content
2 of 3
edited body
Paul
  • 111
  • 3

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 (window, document, undefined) {

    var Mui = function () {
        //the cached instance
        var instance;
        
        //rewrite the constructor
        Mui = function() {
            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'
        };
    };

    Mui.prototype.sayHi = function () {
        alert('hi');
    };

    return Mui;

}(window, this.document));

var mui_1 = new Mui();
var mui_2 = new Mui();
mui_1 === mui_2; //true
Paul
  • 111
  • 3