There are three types of class functions. In this section we discuss the third type, which is static functions. Like static variables, static functions are shared functions 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 and static functions, Javascript has no such thing, and CrxOop provides the global keyword 'crx_static', and the class keyword 'STATIC' to access those functions. However, unlike STATIC, crx_static can only be used to access public static functions, not private ones. Inside static funtions, you can also use 'this.STATIC' to access private static functions. Hence, Inside your instance functions, instance virtual functions and static functions, use 'this.STATIC.somePublicOrPrivateStaticFunction()', and elsewhere use crx_static.
Note that inside static functions, 'this', 'this.O', the return of 'this.O()', and 'this.STATIC', must never be returned from the static functions, or passed to other functions as parameters.
crx_registerClass("ExampleClass",
{
PUBLIC:
{
STATIC:
{
VARS:
{
"publicStaticVar": 1
},
FUNCTIONS:
{
"test": function(pExampleClass)
{
// Notice the usage of this.O to access the private variables of an instance
// from the same class
console.log(this.STATIC.publicStaticVar + "," +
this.STATIC.privateStaticVar + "," +
this.O(pExampleClass).privateVar);
// Could have also used crx_static to access a public static member
// from the same class
console.log(crx_static("ExampleClass").publicStaticVar + "," +
this.STATIC.privateStaticVar + "," +
this.O(pExampleClass).privateVar);
}
}
}
},
PRIVATE:
{
VARS:
{
"privateVar": 5
},
STATIC:
{
VARS:
{
"privateStaticVar": 3
}
}
}
});
var instance = crx_new("ExampleClass");
crx_static("ExampleClass").test(instance);
crx_registerClass("ExampleClass",
{
"VERBOSE": 1,
"public static var publicStaticVar": 1,
"public static function test": function(pExampleClass)
{
// Notice the usage of this.O to access the private variables of an instance
// from the same class
console.log(this.STATIC.publicStaticVar + "," +
this.STATIC.privateStaticVar + "," +
this.O(pExampleClass).privateVar);
// Could have also used crx_static to access a public static member
// from the same class
console.log(crx_static("ExampleClass").publicStaticVar + "," +
this.STATIC.privateStaticVar + "," +
this.O(pExampleClass).privateVar);
},
"private var privateVar": 5,
"private static var privateStaticVar": 3
});
var instance = crx_new("ExampleClass");
crx_static("ExampleClass").test(instance);