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". To understand how to access class private instance variables across class instances refer to the section about 'O'.
crx_registerClass("ExampleClass",
{
PUBLIC:
{
VARS:
{
"publicVar1": 5,
"publicVar2": 6
}
},
PRIVATE:
{
VARS:
{
"privateVar1": 7,
"privateVar2": 8
}
}
});
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:
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);
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);
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": {}
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().