HTML and CSS Reference
In-Depth Information
Listing 7.39 A simpler _ super implementation
function _super(object, methodName) {
var method = object._super && object._super[methodName];
if (typeof method != "function") {
return;
}
// Remove the first two arguments (object and method)
var args = Array.prototype.slice.call(arguments, 2);
// Pass the rest of the arguments along to the super
return method.apply(object, args);
}
Listing 7.40 shows an example of using the _ super function.
Listing 7.40 Using the simpler _ super helper
function LoudPerson(name) {
_super(this, "constructor", name);
}
LoudPerson.inherit(Person);
LoudPerson.prototype.getName = function () {
return _super(this, "getName").toUpperCase();
};
LoudPerson.prototype.say = function (words) {
return _super(this, "speak", words) + "!!!";
};
var np = new LoudPerson("Chris");
assertEquals("CHRIS", np.getName());
assertEquals("Hello!!!", np.say("Hello"));
This is unlikely to be faster to call than spelling out the method to call directly,
but at least it defeats the worst performance issue brought on by implementing
_ super as a method. In general, we can implement sophisticated object oriented
solutions without the use of the _ super crutch, as we will see both throughout this
chapter and the sample projects in Part III, Real-World Test-Driven Development in
JavaScript.
 
Search WWH ::




Custom Search