John Resig的js類繼承的實現

/* 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 = true;
    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 existing 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 that is the same method
            // but on the super-class
            this._super = _super[name];

            // The method only need to be bound temporarily, so we
            // remove it when we're done 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 is actually done in 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]
            }
        }
    }
}

/**
 * 一個緩存構造器的方法
 * @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
}

/**
 * 冒泡排序
 * @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;
                }
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章