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;
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值