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