Search code examples
javascriptjavascript-objectsjavascript-namespaces

Create object of namespace Javascript singleton class


I am new to IIFE and trying to implement namespace in JavaScript on a Siungleton JavaScript class:

I have a JavaScript class (say main class):

var myIIFE = (function () {
    var x = null;

    //constructor function
    var myIIFE = function() {
        var a = new IIFE.InsideIIFE();  //says not a constructor
    }

    myIIFE.prototype = {
        //some methods
    }

   function createIstance() {
        return new myIIFE();
   }

    return {
       getInstance: function() {
           if (!this.instance) {
                this.instance = createInstance();
           }
           return this.instance;
       }
    };
})();

Then I have another JavaScript namespaced class:

myIIFE.InsideIIFE = (function() {
     var inside = function() {}
     inside.prototype = { //some methods }
     return inside;
});

I want to create an object of myIIFE.InsideIIFE in myIIFE, and this is throwing me an error:

myIIFE.InsideIIFE is not a constructor

Am I doing something wrong? Or if this is a correct approach then what changes I should make.

I tried using new this.InsideIIFE() and just InsideIIFE, but non of them worked.


edit:

From my analysis, I understand that myIIFE (the parent) is an object as it return an object at:

 return {
     getInstance: function() {
          //method body
     }
 }

Solution

  • There are many issues with this code, let's try to run it in our heads, from the start:

    var myIIFE = (function () {
     ....
    })();
    

    This results in myIIFE being an object, to be precise, this object:

    {
       getInstance: function() {
           if (!this.instance) {
                this.instance = createInstance();
           }
           return this.instance;
       }
    }
    

    So, then I assume, you do

    myIIFE.getInstance()
    

    Which tries to return new myIIFE();

    It runs into myIIFE() and tries to do this:

    new IIFE.InsideIIFE();
    

    I assume you meant to write

    new myIIFE.InsideIIFE();
    

    because IIFE is not defined it anywhere in the code you provided.

    Let's see what is myIIFE.insideIIFE

    var myIIFE.InsideIIFE = (function() {
         var inside = function() {}
         inside.prototype = { //some methods }
         return inside;
    });
    

    First of all you start with var, which is wrong, because myIIFE is already defined and you are just adding a new property to it. so it should be simply

    myIIFE.InsideIIFE = (function() {
         var inside = function() {}
         inside.prototype = { //some methods }
         return inside;
    });
    

    and it should work.

    In general, it seems by your code, like you have not grasped the whole "constructor function" concept very well. I would suggest you take look at the following links, they should help.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript