There are three types of class functions. In this section we discuss the first type, which are non static non virtual functions.
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());
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());
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.