'this' and 'THIS' are perhaps the most important of the class keywords to understand. Internally, a class instance consists of a public memory sector, and a private memory sector. The 'this' keyword, which is the native Javascript 'this' keyword has access to both the public and private memory sectors. 'THIS' on the other hand has access only to the public memory sector. This is why passing 'this' to functions as parameters, or from functions as returns, is dangerous and must never be done. Instead you would pass around 'THIS'.
The same rule apples to 'this' inside static functions, but being static there is no 'THIS' to pass around. Passing 'this' from inside static functions would leak 'this.O' and 'this.STATIC'.
crx_registerClass("ExampleClass",
{
PUBLIC:
{
VARS:
{
"publicVar": 1
},
FUNCTIONS:
{
"publicFunction": function(pExampleClass)
{
// Remember 'THIS' is a class keyword and hence is found on
// 'this' and can be accessed using either 'this':
console.log(this.THIS.publicVar);
// although for clarity you should never use 'THIS' to
// access instance members but instead use
console.log(this.publicVar);
// Or the object that is the class instance:
console.log(pExampleClass.THIS.publicVar);
// but again you should never use 'THIS' to access
// instance members but instead use
console.log(pExampleClass.publicVar);
// 'THIS' is only meant for passing around. When wanting to
// return the instance from functions, instead of the
// dangerous code "return this", you would use
return this.THIS;
},
"publicFunction2": function()
{
// And when wanting to pass the instance to functions,
// instead of passing 'this', you would pass
// "this.THIS" like the following
var vReturn = this.publicFunction(this.THIS);
// Imagine the danger of the following if publicFunction
// above returned 'this' instead of 'THIS'
return vReturn;
}
}
}
});
crx_registerClass("ExampleClass",
{
"VERBOSE": 1,
"public var publicVar": 1,
"public function publicFunction": function(pExampleClass)
{
// Remember 'THIS' is a class keyword and hence is found on
// 'this' and can be accessed using either 'this':
console.log(this.THIS.publicVar);
// although for clarity you should never use 'THIS' to
// access instance members but instead use
console.log(this.publicVar);
// Or the object that is the class instance:
console.log(pExampleClass.THIS.publicVar);
// but again you should never use 'THIS' to access
// instance members but instead use
console.log(pExampleClass.publicVar);
// 'THIS' is only meant for passing around. When wanting to
// return the instance from functions, instead of the
// dangerous code "return this", you would use
return this.THIS;
},
"public function publicFunction2": function()
{
// And when wanting to pass the instance to functions,
// instead of passing 'this', you would pass
// "this.THIS" like the following
var vReturn = this.publicFunction(this.THIS);
// Imagine the danger of the following if publicFunction
// above returned 'this' instead of 'THIS'
return vReturn;
}
});
'this' and 'THIS' have another use in constructors, although this usage should be kept to a minimal. During construction, if you recall, the object is not locked yet and you can continue adding members to it. In the constructor, if you add members to 'this', you create private variables only, even if they are functions. If you add members to 'THIS', you create public variables only, even if they are functions. We only condone, but in rare cases, this usage to create private and public final variables (C++), or const variables (Java). For example:
crx_registerClass("ExampleClass",
{
PUBLIC:
{
CONSTRUCT: function()
{
Object.defineProperty(this, "privateFinalVariable", {"value": 3, "writable": false});
Object.defineProperty(this.THIS, "publicFinalVariable", {"value": 5, "writable": false});
}
}
});
var instance = crx_new("ExampleClass");
console.log(instance.privateFinalVariable);
console.log(instance.publicFinalVariable);
crx_registerClass("ExampleClass",
{
"VERBOSE": 1,
"public CONSTRUCT": function()
{
Object.defineProperty(this, "privateFinalVariable", {"value": 3, "writable": false});
Object.defineProperty(this.THIS, "publicFinalVariable", {"value": 5, "writable": false});
}
});
var instance = crx_new("ExampleClass");
console.log(instance.privateFinalVariable);
console.log(instance.publicFinalVariable);