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):
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:
We could also have done the above by adding the following to the Relator class:
and then use the following method on Person (instead of the previous implementation):
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).
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.set(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 = name;
}
Person.prototype.getName = function () {var _ = __.method(this);
alert('My name is '+_.name);
};
Person.prototype.zombie = function () {
var zombies = __.getAll();
zombies.forEach(function(zombie){
zombie.that.getName();
// convenient syntax as we could also do something with zombie.name (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();
zombies.forEach(function(zombie){
zombie.getName();
// var z = __.get(zombie); // Getting a private variable now requires an extra step
// alert(z.name);
});
};
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).
0 Comments:
Post a Comment
<< Home