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.2 Public and Private Instance Variables

Instance variables are variables available to class instances only. The library supports both public and private accessor types. Class instance variables can be accessed using "this", or the instance object. Needless to say class private instance variables can only be accessed using "this".

Example: Instance Variables
JS (Tree)
crx_registerClass("ExampleClass",
{
   PUBLIC:
   {
      VARS:
      {
         "publicVar1": 5,
         "publicVar2": 6
      }
   },
   PRIVATE:
   {
      VARS:
      {
         "privateVar1": 7,
         "privateVar2": 8
      }
   }
});
JS (Verbose)
crx_registerClass("ExampleClass",
{
   "VERBOSE": 1,
   "public var publicVar1": 5,
   "public var publicVar2": 6,
   "private var privateVar1": 7,
   "private var privateVar2": 8
});

Instantiation of class instance variables can take place in the definition as shown above, however it is important to understand that the statement in the definition with the instantiation is executed only once, and further more when an instance is created the result is simply copied over using the assignment operator. Consider the following example:

Example: Instance Variables Instantiation
JS (Tree)
crx_registerClass("ExampleClass",
{
   PUBLIC:
   {
      VARS:
      {
         "publicVar": {}
      }
   }
});

var instance1 = crx_new("ExampleClass");
var instance2 = crx_new("ExampleClass");

instance1.publicVar['someProperty'] = 10;
console.log(instance2.publicVar.someProperty);
10
JS (Verbose)
crx_registerClass("ExampleClass",
{
   "VERBOSE": 1,
   "public var publicVar": {}
});

var instance1 = crx_new("ExampleClass");
var instance2 = crx_new("ExampleClass");

instance1.publicVar['someProperty'] = 10;
console.log(instance2.publicVar.someProperty);
10

You might have expected the above to print "undefined", yet it printed 10. This is because both instance1.publicVar and instance2.publicVar are pointing to the same object. The statement

	"public var publicVar": {}

in the definition executed only once, and the result, a pointer to the object, was copied over for both instances. If you wanted a new object for each instance publicVar variable, you should instantiate the variable in the constructor, but the variable should still be declared in the definition.

WARNING: Assigning Javascript functions to private instance variables is dangerous and must never be done. Assigning Javascript functions to public instance variables should be safe, but can lead to unexpected results, and should also be avoided. For more information see the section on security and crxOop.var().