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

5.4.3 Shared Private Instance Variables and Functions

Shared private structure members can be accessed from any structure function within the instance, regardless of the structure. Further more, like the shared public members, structure instances contain only one copy of each shared private member. The one last decalred in the inheritance chain. Shared private structure members can be thought of as protected 'virtual' class members, both functions and variables, roughly. It is recommended that you refer to the section about 'O'.

Example: Shared Public Access Members
JS (Tree)
crx_registerStructure("ExampleStructureA",
{
   SHARED:
   {
      PRIVATE:
      {
         VARS:
         {
            "sharedPrivateVar": "I am ExampleStructureA::sharedPrivateVar",
            "sharedPrivateVar2": "I am ExampleStructureA::sharedPrivateVar2",
         },
         FUNCTIONS:
         {
            "sharedPrivateFunction": function()
            {
               return "I am ExampleStructureA::sharedPrivateFunction()";
            }
         }
      },
      PUBLIC:
      {
         FUNCTIONS:
         {
            "testFromExampleStructureA": function()
            {
               console.log(this.sharedPrivateVar);
               console.log(this.sharedPrivateFunction());
            }
         }
      }
   }
});
crx_registerStructure("ExampleStructureB",
{
   INHERITS: ["ExampleStructureA"],
   SHARED:
   {
      PRIVATE:
      {
         VARS:
         {
            "sharedPrivateVar": "I am ExampleStructureB::sharedPrivateVar",
         },
         FUNCTIONS:
         {
            "sharedPrivateFunction": function()
            {
               return "I am ExampleStructureB::sharedPrivateFunction()";
            }
         }
      },
      PUBLIC:
      {
         FUNCTIONS:
         {
            "testFromExampleStructureB": function()
            {
               console.log(this.sharedPrivateVar);
               console.log(this.sharedPrivateFunction());

               //   Notice how we can still access the shared 'private' variable
               //      that is defined in ExampleStructureA only. This applies
               //      to shared private functions too.
               console.log(this.sharedPrivateVar2);
            }
         }
      }
   }
});

var a = crx_new("ExampleStructureB");

console.log("From out side the instance functions");
console.log(a.sharedPrivateVar);
console.log(a.sharedPrivateFunction);
console.log("From inside ExampleStructureA function in an ExampleStructureB instance");
a.testFromExampleStructureA();
console.log("From inside ExampleStructureB function in an ExampleStructureB instance");
a.testFromExampleStructureB();
From out side the instance functions
undefined
undefined
From inside ExampleStructureA function in an ExampleStructureB instance
I am ExampleStructureB::sharedPrivateVar
I am ExampleStructureB::sharedPrivateFunction()
From inside ExampleStructureB function in an ExampleStructureB instance
I am ExampleStructureB::sharedPrivateVar
I am ExampleStructureB::sharedPrivateFunction()
I am ExampleStructureA::sharedPrivateVar2
JS (Verbose)
crx_registerStructure("ExampleStructureA",
{
   VERBOSE: 1,
   "shared private var sharedPrivateVar": "I am ExampleStructureA::sharedPrivateVar",
   "shared private var sharedPrivateVar2": "I am ExampleStructureA::sharedPrivateVar2",
   "shared private function sharedPrivateFunction": function()
   {
      return "I am ExampleStructureA::sharedPrivateFunction()";
   },
   "shared public function testFromExampleStructureA": function()
   {
      console.log(this.sharedPrivateVar);
      console.log(this.sharedPrivateFunction());
   }
});
crx_registerStructure("ExampleStructureB",
{
   VERBOSE: 1,
   INHERITS: ["ExampleStructureA"],
   "shared private var sharedPrivateVar": "I am ExampleStructureB::sharedPrivateVar",
   "shared private function sharedPrivateFunction": function()
   {
      return "I am ExampleStructureB::sharedPrivateFunction()";
   },
   "shared public function testFromExampleStructureB": function()
   {
      console.log(this.sharedPrivateVar);
      console.log(this.sharedPrivateFunction());

      //   Notice how we can still access the shared 'private' variable
      //      that is defined in ExampleStructureA only. This applies
      //      to shared private functions too.
      console.log(this.sharedPrivateVar2);
   }
});

var a = crx_new("ExampleStructureB");

console.log("From out side the instance functions");
console.log(a.sharedPrivateVar);
console.log(a.sharedPrivateFunction);
console.log("From inside ExampleStructureA function in an ExampleStructureB instance");
a.testFromExampleStructureA();
console.log("From inside ExampleStructureB function in an ExampleStructureB instance");
a.testFromExampleStructureB();
From out side the instance functions
undefined
undefined
From inside ExampleStructureA function in an ExampleStructureB instance
I am ExampleStructureB::sharedPrivateVar
I am ExampleStructureB::sharedPrivateFunction()
From inside ExampleStructureB function in an ExampleStructureB instance
I am ExampleStructureB::sharedPrivateVar
I am ExampleStructureB::sharedPrivateFunction()
I am ExampleStructureA::sharedPrivateVar2