/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: * ******************************************************************* * Project: Inheritance * * Author: John Resig * ******************************************************************* * JavaScript "Class" constructor and inheritance * Includes: * Class Class.extend( Object classData ); * | __constructor - Class constructor; * | __construct - Class initialization procedure. * | _parent - Call method of parental class. * * */ /**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**/ (function(){ var ___Inheritance_Initializing = false, ___Inheritance_fnTest = /xyz/.test(function(){xyz;}) ? /\b_parent\b/ : /.*/; this.Class = function(){}; Class.extend = function( classData ){ var _parent = this.prototype; ___Inheritance_Initializing = true; var prototype = new this(); ___Inheritance_Initializing = false; for( var k in classData ){ prototype[k] = typeof( classData[k] ) == 'function' && typeof( _parent[k] ) == 'function' && ___Inheritance_fnTest.test( classData[k] ) ? (function( k, fn ){ return function(){ var tmp = this._parent; this._parent = _parent[k]; var ret = fn.apply(this, arguments); this._parent = tmp; return ret; }; })( k, classData[k] ) : classData[k]; } function Class(){ if( !___Inheritance_Initializing && this.__construct ){ this.__construct.apply( this, arguments ); } } Class.prototype = prototype; Class.__constructor = Class; Class.extend = arguments.callee; return Class; }; })();