Advertisement
Guest User

Untitled

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