Brett's Blog
Edit     New

Thursday, February 19, 2009

Enhancing Relator and Extending Classes using Relator for private instance variables

One addition to my previous blog post...

These two methods on the Relator could be interesting:

getAll: function () { // Be able to read or manipulate ALL instances within any instance (kind of violates privacy, but offers what I call the "Borg" option)
return Array;
readonly : function (obj) { // Call, especially from an extending class, to get access to the original class' private/protected variables without fear of manipulating them
var newObj = {};
for (var i in Array[Stack.indexOf(obj)]) {
newObj[i] = Array[Stack.indexOf(obj)][i];
return newObj;
// ...OR...
// return function (value) {return Array[Stack.indexOf(obj)][value];} // copies of scalars, not references

Then your extending class could look something like this (the following also demonstrates protected variables and extending classes):

var ExtClass = (function (ParentClass) { // We pass in the parent class as an argument, so we can copy-paste the rest of the code, always using "ParentClass" in the closure.

// var _extendedPrivate = Relator.$(); // we could define our own set of private variables for this extended class to,

function ExtClass (){; // We have to call the parent constructor in order to extend the class in a way that its public methods operating on private variables there still work--if you still want a constructor in the base class with side effects not triggered by extending classes, put a conditional in the constructor and pass in (or don't pass in) a certain argument or type of argument to avoid the rest of the set-up besides the necessary line "_private.set(this);".
/* We could also define and use private variables for just inside this extending class
_extendedPrivate.set(this);var _ = _extendedPrivate.get(this);
_.extendedPrivateVar = 5;
var _protect = _private.get(this); // Let's us use the "private" variables from the base class here too (note that "_private" must be accessible somehow for us to do this--either this extending class should be defined in the same closure as the base class (more like protected access since the extending class CAN access the variables if it wishes), or it could be passed in as an argument/a global (more like a public variable since public code could also access it)
_protect.privateInstanceVar = 7; // We can manipulate a private instance set up in the base class (the private become protected when we access them this way)
var _readonly = _private.readonly(this); // allows read-only protected access of the private variables in the base class
alert(_readonly.privateInstanceVar); // Gets base class' privateInstanceVar but won't be able to set it (except as a copy here)
// alert(_readonly('protectedInstanceVar')); // Or using the "return function" approach commented out in readonly() to do the same
ExtClass.prototype = new ParentClass; // ParentClass also gets called here (again, you can use a conditional in the parent class to avoid repeating any unnecessary set-up code)
ExtClass.prototype.constructor = ExtClass;

return ExtClass;

})(MyClass); // Just need to add specific base class here, then use the generic code above


Post a Comment

<< Home

Brett's Blog Web