CrxOop CrxOop: Bringing Object Oriented Programming, and Proper Prototype Based Programming, To Javascript
The aim of the library documented below is to provide developers with a solution to allow development using interfaces and classes as they are known in other object oriented programming (OOP) languages such as C++, C#, Java and PHP. Further more, V1.4 introduces structures, a generalization of the concept of prototypal inheritance, known here as POBP.
Subject
Body*
Email
SUBMIT CLOSE
Menu

3.3.3 Public, Protected, and Private Functions

There are three types of class functions. In this section we discuss the first type, which are non static non virtual functions.

Example: Public, Protected And Private Functions
JS (Tree)
crx_registerClass("ExampleClass",
{
   PUBLIC:
   {
      VARS:
      {
         "publicVar": 5,
      },
      FUNCTIONS:
      {
         "publicFunction1": function()
         {
            console.log("publicFunction1: " + this.publicVar + ", " + this.privateVar);
         },
         "publicFunction2": function()
         {
            this.privateFunction1();
            this.privateFunction2(20);

            this.protectedFunction1();
            this.protectedFunction2(21);

            return "publicFunction2";
         }
      }
   },
   PROTECTED:
   {
      FUNCTIONS:
      {
         "protectedFunction1": function()
         {
            console.log("protectedFunction1: " + this.publicVar + ", " + this.privateVar);
         },
         "protectedFunction2": function(pA)
         {
            console.log("protectedFunction2: " + pA);
         }
      }
   },
   PRIVATE:
   {
      VARS:
      {
         "privateVar": 7
      },
      FUNCTIONS:
      {
         "privateFunction1": function()
         {
            console.log("privateFunction1: " + this.publicVar + ", " + this.privateVar);
         },
         "privateFunction2": function(pA)
         {
            console.log("privateFunction2: " + pA);
         }
      }
   }
});
var instance = crx_new("ExampleClass");

instance.publicFunction1();
console.log(instance.publicFunction2());
publicFunction1: 5, 7
privateFunction1: 5, 7
privateFunction2: 20
protectedFunction1: 5, 7
protecredFunction2: 21
publicFunction2
JS (Verbose)
crx_registerClass("ExampleClass",
{
   "VERBOSE": 1,
   "public var publicVar": 5,
   "public function publicFunction1": function()
   {
      console.log("publicFunction1: " + this.publicVar + ", " + this.privateVar);
   },
   "public function publicFunction2": function()
   {
      this.privateFunction1();
      this.privateFunction2(20);

      this.protectedFunction1();
      this.protectedFunction2(21);

      return "publicFunction2";
   },
   "protected function protectedFunction1": function()
   {
      console.log("protectedFunction1: " + this.publicVar + ", " + this.privateVar);
   },
   "protected function protectedFunction2": function(pA)
   {
      console.log("protectedFunction2: " + pA);
   },
   "private var privateVar": 7,
   "private function privateFunction1": function()
   {
      console.log("privateFunction1: " + this.publicVar + ", " + this.privateVar);
   },
   "private function privateFunction2": function(pA)
   {
      console.log("privateFunction2: " + pA);
   }
});
var instance = crx_new("ExampleClass");

instance.publicFunction1();
console.log(instance.publicFunction2());
publicFunction1: 5, 7
privateFunction1: 5, 7
privateFunction2: 20
protectedFunction1: 5, 7
protecredFunction2: 21
publicFunction2

Inside these functions, "this" points to the instance and can be used to access public, protected and private instance functions and virtual functions, and public and private variables as seen above. Needless to say, this is still javascript, so 'this' could end up pointing elsewhere inside these functions if it is used inside an inner function, such as an anonymous function created during the function run.

Note that "this", and other Class Keywords, are not safe to pass around as function parameters and returns. More on that later.

To understand how to access class private and protected instance functions across class instances refer to the section about 'O'.

Note that being non virtual functions, these functions, like class instance variables, suffer from name hiding.