1 goog.provide('Banana.Util.Base'); 2 3 /* Simple JavaScript Inheritance 4 * By John Resig http://ejohn.org/ 5 * MIT Licensed. 6 */ 7 // Inspired by base2 and Prototype 8 (function(){ 9 var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/; 10 // The base Class implementation (does nothing) 11 this.Class = function(){}; 12 13 // Create a new Class that inherits from this class 14 Class.extend = function(prop) { 15 var _super = this.prototype; 16 17 // Instantiate a base class (but only create the instance, 18 // don't run the init constructor) 19 initializing = true; 20 var prototype = new this(); 21 initializing = false; 22 23 // Copy the properties over onto the new prototype 24 for (var name in prop) { 25 // Check if we're overwriting an existing function 26 prototype[name] = typeof prop[name] == "function" && 27 typeof _super[name] == "function" && fnTest.test(prop[name]) ? 28 (function(name, fn){ 29 return function() { 30 var tmp = this._super; 31 32 // Add a new ._super() method that is the same method 33 // but on the super-class 34 this._super = _super[name]; 35 36 // The method only need to be bound temporarily, so we 37 // remove it when we're done executing 38 var ret = fn.apply(this, arguments); 39 this._super = tmp; 40 41 return ret; 42 }; 43 })(name, prop[name]) : 44 prop[name]; 45 } 46 47 // The dummy class constructor 48 function Class() { 49 // All construction is actually done in the init method 50 if ( !initializing && this.init ) 51 this.init.apply(this, arguments); 52 } 53 54 // Populate our constructed prototype object 55 Class.prototype = prototype; 56 57 // Enforce the constructor to be what we expect 58 Class.prototype.constructor = Class; 59 60 // And make this class extendable 61 Class.extend = arguments.callee; 62 63 return Class; 64 }; 65 })(); 66