The implementation of John Resig's JS class inheritance __js

Source: Internet
Author: User
/* Simple JavaScript Inheritance * by John resig http://ejohn.org/* MIT licensed. ///Inspired by BASE2 and Prototype (function () {var initializing = false, Fntest =/xyz/.test (function () {xyz;})?/\b

  _super\b/:/.*/; The base Class implementation (does nothing) this.

  Class = function () {};

    Create a new Class that inherits from this class class.extend = function (prop) {var _super = This.prototype; Instantiate a base class (but only create the instance,//don ' t run the init constructor) initializing = tr
    Ue
    var prototype = new This ();

    initializing = false; Copy the properties over onto the "new prototype for" (Var name in prop) {//Check if we ' re overwriting an Exi Sting function Prototype[name] = typeof Prop[name] = = "function" && typeof _super[name] = "function
        "&& fntest.test (Prop[name])?"

   (function (name, FN) {return function () {var tmp = This._super;         Add a new. _super () method, the same method//But on the Super-class this._sup

            ER = _super[name]; The method is need to is bound temporarily, so we/remove it when we do executing var
            RET = fn.apply (this, arguments);

            This._super = tmp;
          return ret;
        };
    }) (name, Prop[name]): Prop[name]; 
      }//The Dummy class constructor function class () {//All construction are actually done with the Init method
    if (!initializing && this.init) this.init.apply (this, arguments);

    }//Populate Our constructed prototype object Class.prototype = prototype;

    Enforce the constructor to be what we expect Class.prototype.constructor = Class;

    And make this class extendable class.extend = Arguments.callee;
  return Class;
};

})();
    function ext (newclass, T, i) {var n; if (typeof Newclass = = = "string") {n = newclass;
        Newclass = R.getclass (Newclass)} if (t) {var a = function () {};
        A.prototype = T.prototype;
        Newclass.prototype = new A;
        NewClass.prototype.constructor = Newclass;
        Newclass.superclass = T.prototype;
    if (T.prototype.constructor = = Object.prototype.constructor) {t.prototype.constructor = t}}
        if (n) {newClass.prototype.getClassName = function () {return n}} if (i) {
        var o = newclass.prototype;
        For (var A in i) {S.ext (A, O, i)} var l = r.es.length;
            for (var h = 0; h < l; h++) {A = r.es[h]; 
if (I.hasownproperty (a) &&!i.propertyisenumerable (a)) {O[a] = I[a]}}
    /** * A cache constructor method * @param T * @returns {*} */function GetClass (t) {var i = r.classcache[t];
if (!i) {        var n = t.split (".");
        var a = N.length;
        i = e;
 for (var o = 0; o < A; o++) {i = I[n[o]] [r.classcache[t] = i} return i}/**
    * Bubble sort * @param ar * @param [fn]/function bubblesort (AR, fn) {var len = ar.length;
    var done = false; for (var I=len;--i>0;)
        {if (done) {return;
            else {done = true; for (var j=-1; ++j<i;)
                {if (FN) {var r = fn (Ar[j], ar[j+1]);
                else {var r = Ar[j] > ar[j+1];
                    } if (r > 0) {var tmp = ar[j+1];
                    AR[J+1] = Ar[j];
                    AR[J] = tmp;
                Done = false; }
            }
        }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.