1 /* Simple JavaScript Inheritance
2 * By John Resig http://ejohn.org/
5 // Inspired by base2 and Prototype
7 var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
9 // The base Class implementation (does nothing)
10 this.Class = function(){};
12 // Create a new Class that inherits from this class
13 Class.extend = function(prop) {
14 var _super = this.prototype;
16 // Instantiate a base class (but only create the instance,
17 // don't run the init constructor)
19 var prototype = new this();
22 // Copy the properties over onto the new prototype
23 for (var name in prop) {
24 // Check if we're overwriting an existing function
25 prototype[name] = typeof prop[name] == "function" &&
26 typeof _super[name] == "function" && fnTest.test(prop[name]) ?
29 var tmp = this._super;
31 // Add a new ._super() method that is the same method
32 // but on the super-class
33 this._super = _super[name];
35 // The method only need to be bound temporarily, so we
36 // remove it when we're done executing
37 var ret = fn.apply(this, arguments);
42 })(name, prop[name]) :
46 // The dummy class constructor
48 // All construction is actually done in the init method
49 if ( !initializing && this.init )
50 this.init.apply(this, arguments);
53 // Populate our constructed prototype object
54 Class.prototype = prototype;
56 // Enforce the constructor to be what we expect
57 Class.prototype.constructor = Class;
59 // And make this class extendable
60 Class.extend = arguments.callee;