Here is a Prototype extension that allows one to extend a class in an object-oriented fashion. In particular, “initialize” methods are chained so that an initializer can pass control to the initializer of its superclass and on up to the root of the inheritance hierarchy.
var MyDerivedClass = Class.extend( MyBaseClass, { initialize: function( q ) { this.q = q; }, z: "z", y: "y" }); var MyDeeperDerivedClass = Class.extend( MyDerivedClass, { initialize: function( q, r) { this.superInit(q); this.r = r; }, y: "deeper-y" });
In the above, creating an instance of MyDeeperDerivedClass with arguments (“one”,”two”) will result in an object with this.q = “one”, this.r = “two”, this.z = “z”, and this.y = “deeper-y”.
In addition, it redefines Class.create to accept an argument consisting of the prototype of the new class, thus allowing declarations like:
var MyBaseClass = Class.create( { a:"a", b:"b" } );
instead of:
var MyBaseClass = Class.create(); MyBaseClass.prototype = { a:"a", b:"b" };
Without an argument, Class.create behaves like it does in Prototype-except that a trivial “initialize” member is created if one is not provided in the argument prototype.
A test page is here
Enjoy.
-=greg