Om3ga
Om3ga

Reputation: 32913

Uncaught ReferenceError: ModuleA is not defined

I am using following code which uses prototypal inheritance. Somehow when I create a new instance of ModuleA then it return

Uncaught ReferenceError: ModuleA is not defined

Below is my code

(function () {
  var ModuleA = function () {
    console.log('ModuleA');
  };

  ModuleA.prototype = (function () {
    var moduleA = function () {

    };

    return {
      moduleA: moduleA
    }
  }());

  return ModuleA;

})();

new ModuleA();

UPDATE

JSFIDDLE

Upvotes: 0

Views: 31

Answers (1)

Sam Bauwens
Sam Bauwens

Reputation: 1377

It's because you explicitely put your ModuleA declaration in an IIFE which will hide everything inside. So ModuleA is in the scope of your IIFE. You did return return ModuleA but you didn't put it anywhere.

Do this instead :

var ModuleA = (function () {
  var ModuleA = function () {
    console.log('ModuleA');
  };

  ModuleA.prototype = (function () {
    var moduleA = function () {

    };

    return {
      moduleA: moduleA
    }
  }());

  return ModuleA;

})();

new ModuleA();

Upvotes: 1

Related Questions