Brett's Blog
Edit     New

Monday, February 23, 2009

Relator Zombies

I referred to the "Borg" option in the code comments for this post, in that we can call getAll() on the Relator for any object to access and manipulate any or all private instances variables for other objects already created. Thus, one can create an object, create another object, and then use the latter object to change data on all previous objects.

If we add a 'that' property to all of our Relator instances (easily achievable by adding one line to the middle of the "constructor" method we added for Relator in our last post):

constructor : function (that) {
this.get(that).that = that;
return this.get(that); // Assign to var _ = __.constructor(this);

we can then reference any public methods or members on all objects of this class (along with their private variables) after calling getAll() on the Relator instance:

var Person = (function () {var __ = Relator.$();
function Person (name) {var _ = __.constructor(this); = name;
Person.prototype.getName = function () {var _ = __.method(this);
alert('My name is ';
Person.prototype.zombie = function () {
var zombies = __.getAll();
// convenient syntax as we could also do something with (or access other private instance variables we added)
return Person;

var bob = new Person('Bob');
var sarah = new Person('Sarah');
sarah.zombie(); // Alerts "My name is Bob" and then "My name is Sarah"

We could also have done the above by adding the following to the Relator class:

getInstances : function () {
return Stack;

and then use the following method on Person (instead of the previous implementation):

Person.prototype.zombie = function () {
var zombies = __.getInstances();
// var z = __.get(zombie); // Getting a private variable now requires an extra step
// alert(;

This syntax is shorter to access the whole object and its public methods/members, but does not allow us to use the same syntax to access the zombies' private instances (unless we assign "__.get(zombie);" to a variable within the forEach and access the private data from there, as in the commented out text above).


Post a Comment

<< Home

Brett's Blog Web