HTML and CSS Reference
In-Depth Information
creates a wrapper function that temporarily sets the
this._super
method to the parent's definition during
the call:
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
The preceding code checks if the property already exists on the superclass; if it does, it creates a function that
does the temporary
this._super
reassignment before calling the new method again. If the method doesn't
exist, the code simply assigns the property, preventing adding in any additional overhead.
The
Class
code also adds in a constructor function that automatically calls the
init()
method of the ob-
ject, allowing for the chaining of initializers as well:
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
Finally, it adds the
extend
method to the class so that it can further be subclassed:
// And make this class extendable
Class.extend = arguments.callee;
Calling
arguments.callee
returns the method that was called (in this case
extend
) and that method
is then assigned to the property
extend
of the returned
Class
object, allowing further subclassing down the
line.