tarabyte
tarabyte

Reputation: 19172

Unable to call 'private' method from javascript namespace?

I'm trying to understand how I can break down functionality by files in javascript. I'm trying to think of namespaces as singleton classes. I don't understand why in the code below, MODULE_A.privateMethod1() throws the error: Uncaught TypeError: undefined is not a function. When I open up the logged object, I can't see the private methods there either.

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Test Project</title>

    <script src="index.js"></script>
    <script src="moduleA.js"></script>
  </head>
    <body>
      <div>
        hi
      </div>
  </body>
</html>

index.js:

(function() {

  var initializeWindow = function() {
    console.log("initializeWindow");
    MODULE_A.publicMethod1();
  };

  window.addEventListener('load', initializeWindow);
})();

moduleA.js:

// "Singleton Class"
var MODULE_A = (function() {
  // ---------------------------------------------------------------------------
  // "Private" Class Constants
  // ---------------------------------------------------------------------------
  var A_CONSTANT_NUMBER = 1;

  // ---------------------------------------------------------------------------
  // "Private" Class Variables (Properties)
  // ---------------------------------------------------------------------------
  var myPrivateVar_ = 2;

  // ---------------------------------------------------------------------------
  // "Private" Methods
  // ---------------------------------------------------------------------------
  var privateMethod1 = function() {
    console.log("A.privateMethod1");
  };

  var privateMethod2 = function() {
    console.log("A.privateMethod2");
  };

  return {
    // ---------------------------------------------------------------------------
    // "Public" Class Variables (Properties)
    // ---------------------------------------------------------------------------
    aPublicVar1: 3,

    // ---------------------------------------------------------------------------
    // "Public" Methods
    // ---------------------------------------------------------------------------
    publicMethod1: function() {
      console.log("A.publicMethod1");
      this.publicMethod2();
      // ERROR HERE!!!
      this.privateMethod1();
      this.privateMethod2();
    },

    publicMethod2: function() {
      console.log("A.publicMethod2");
    }
  }
})();

enter image description here

Upvotes: 1

Views: 598

Answers (1)

mido
mido

Reputation: 25034

The solution is one of the two :

When you declare something with var, it cannot be accessed with this

either :

this.privateMethod1 = function() {
console.log("A.privateMethod1");
};

or

 publicMethod1: function() {
  console.log("B.publicMethod1");
  this.publicMethod2();
  privateMethod1();
  privateMethod2();
},

edit: if you want the method as private, you should follow method2, you can go to this site for more explanation.

Upvotes: 3

Related Questions