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.5 Public and Private Static Variables, and Constants

Static variables are shared variables among all instances of a particular class. Both public and private accessors are supported. Unlike C++, which has the scope operator "::" to access static variables, Javascript has no such thing, and CrxOop provides the global keyword 'crx_static', and the class keyword 'STATIC' to access those variables. However, unlike STATIC, crx_static can only be used to access public static variables, not private ones. Inside static funtions, you can also use 'this.STATIC' to access private static variables. Hence, Inside your instance functions, instance virtual functions and static functions, use 'this.STATIC.somePublicOrPrivateVariable', and elsewhere use crx_static.

Constants are static variables with constant binding. If the constant was an object, the constant would be the equivilant of a constant pointer in C++ (void * const), and not a pointer to a constant object (void const *). Accessing constants works the same way as accessing other static variables, and follows the same rules mentioned above.

Example: Public and Private Static Variables
JS (Tree)
crx_registerClass("ExampleClass",
{
   PUBLIC:
   {
      CONSTS:
      {
         "publicConstant1": 5,
         "publicConstant2": 6
      },
      STATIC:
      {
         VARS:
         {
            "publicStaticVar1": 1,
            "publicStaticVar2": 2
         }
      },
      FUNCTIONS:
      {
         "test": function()
         {
            console.log(this.STATIC.publicStaticVar2 + "," + this.STATIC.privateStaticVar2);
            
            //   We could have also used crx_static for the public variable 'publicStaticVar2',
            //      which was not necessary here because it is from the same class.
            console.log(crx_static('ExampleClass').publicStaticVar2 + "," + this.STATIC.privateStaticVar2);
            
            
            console.log(this.STATIC.publicConstant2 + "," + this.STATIC.privateConstant1);
            
            //   We could have also used crx_static for the public constant 'publicConstant2',
            //      which was not necessary here because it is from the same class.
            console.log(crx_static('ExampleClass').publicConstant2 + "," + this.STATIC.privateConstant1);
         }
      }
   },
   PRIVATE:
   {
      CONSTS:
      {
         "privateConstant1": 7,
         "privateConstant2": 8
      },
      STATIC:
      {
         VARS:
         {
            "privateStaticVar1": 3,
            "privateStaticVar2": 4
         }
      }
   }
});
var instance = crx_new("ExampleClass");

instance.test();
console.log("(" + crx_static("ExampleClass").publicStaticVar2 + ")");
console.log("(" + crx_static("ExampleClass").publicConstant1 + ")");
console.log("(" + instance.STATIC.publicStaticVar2 + ")");
console.log("(" + instance.STATIC.publicConstant1 + ")");
2,4
2,4
6,7
6,7
(2)
(5)
(2)
(5)
JS (Verbose)
crx_registerClass("ExampleClass",
{
   "VERBOSE": 1,
   "public const publicConstant1": 5,
   "public const publicConstant2": 6,
   "public static var publicStaticVar1": 1,
   "public static var publicStaticVar2": 2,
   "private const privateConstant1": 7,
   "private const privateConstant2": 8,
   "private static var privateStaticVar1": 3,
   "private static var privateStaticVar2": 4,
   "public function test": function()
   {
      console.log(this.STATIC.publicStaticVar2 + "," + this.STATIC.privateStaticVar2);
            
      //   We could have also used crx_static for the public variable 'publicStaticVar2',
      //      which was not necessary here because it is from the same class.
      console.log(crx_static('ExampleClass').publicStaticVar2 + "," + this.STATIC.privateStaticVar2);
      
      
      console.log(this.STATIC.publicConstant2 + "," + this.STATIC.privateConstant1);
      
      //   We could have also used crx_static for the public constant 'publicConstant2',
      //      which was not necessary here because it is from the same class.
      console.log(crx_static('ExampleClass').publicConstant2 + "," + this.STATIC.privateConstant1);
   }
});
var instance = crx_new("ExampleClass");

instance.test();
console.log("(" + crx_static("ExampleClass").publicStaticVar2 + ")");
console.log("(" + crx_static("ExampleClass").publicConstant1 + ")");
console.log("(" + instance.STATIC.publicStaticVar2 + ")");
console.log("(" + instance.STATIC.publicConstant1 + ")");
2,4
2,4
6,7
6,7
(2)
(5)
(2)
(5)

As can be seen above, 'STATIC' can also be used on instances themselves, and gives no access to privates as expected. This usage is more performant, but less clear, and more error prone. With this usage of STATIC, the class type is the type of the instance. If you feel the need to cast the instance to the correct type before using STATIC, use crx_static instead.

Note that on older browsers where defining constant bindings is not possible, each object is provided with its own copy of the constants, increasing memory usage. Furthermore, any usage of crx_static whether to access constants or other statics can be significantly slower on those browser but only on classes that have constants. This is because CrxOop will assert the constants on these browser each time crx_static is used.

WARNING: Assigning Javascript functions to private static variables is dangerous and must never be done. Assigning Javascript functions to public static 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().