HTML and CSS Reference
In-Depth Information
which is an object that defines the methods on
LoudPerson.prototype
that
need to call
_
super
. Listing 7.37 shows one possible implementation.
Listing 7.37
Implementing
_
super
as a method
if (!Function.prototype.inherit2) {
(function () {
function F() {}
Function.prototype.inherit2 = function (superFn, methods) {
F.prototype = superFn.prototype;
this.prototype = new F();
this.prototype.constructor = this;
var subProto = this.prototype;
tddjs.each(methods, function (name, method) {
// Wrap the original method
subProto[name] = function () {
var returnValue;
var oldSuper = this._super;
this._super = superFn.prototype[name];
try {
returnValue = method.apply(this, arguments);
} finally {
this._super = oldSuper;
}
return returnValue;
};
});
};
}());
}
This implementation allows for calls to
this.
_
super()
as if the method had
special meaning. In reality, we're wrapping the original methods in a new function
that takes care of setting
this.
_
super
to the right method before calling the
original method.
Using the new inherit function we could now implement
Sphere
as seen in
Listing 7.38.