All files / mvc klass.js

97.78% Statements 44/45
69.57% Branches 16/23
92.31% Functions 12/13
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                2x 2x     208x       4x 41x 41x 41x 41x 41x 41x   41x   41x         36x 103x 103x                     30x 30x   30x 30x 30x 30x 30x     79x 78x   1x 1x   1x       30x 33x 33x 33x     30x   30x 30x 3x 1x 1x 1x   3x 3x     30x   30x       5x     2x  
/* eslint-disable no-underscore-dangle */
/**
 * class 的 ES5 实现
 * - 为代码通过 eslint 检查做了些修改
 * @module mvc/klass
 * @see [klass](https://github.com/ded/klass)
 */
 
var fnTest = (/xyz/).test(function () { var xyz; return xyz; }) ? (/\bsupr\b/) : (/.*/);
var proto = 'prototype';
 
function isFn(o) {
  return typeof o === 'function';
}
 
function wrap(k, fn, supr) {
  return function () {
    var tmp = this.supr;
    this.supr = supr[proto][k];
    var undef = {}.fabricatedUndefined;
    var ret = undef;
    try {
      ret = fn.apply(this, arguments);
    } finally {
      this.supr = tmp;
    }
    return ret;
  };
}
 
function execProcess(what, o, supr) {
  for (var k in o) {
    Eif (o.hasOwnProperty(k)) {
      what[k] = (
        isFn(o[k])
        && isFn(supr[proto][k])
        && fnTest.test(o[k])
      ) ? wrap(k, o[k], supr) : o[k];
    }
  }
}
 
function extend(o, fromSub) {
  // must redefine noop each time so it doesn't inherit from previous arbitrary classes
  var Noop = function () {};
  Noop[proto] = this[proto];
 
  var supr = this;
  var prototype = new Noop();
  var isFunction = isFn(o);
  var _constructor = isFunction ? o : this;
  var _methods = isFunction ? {} : o;
 
  function fn() {
    if (this.initialize) {
      this.initialize.apply(this, arguments);
    } else {
      Eif (fromSub || isFunction) {
        supr.apply(this, arguments);
      }
      _constructor.apply(this, arguments);
    }
  }
 
  fn.methods = function (obj) {
    execProcess(prototype, obj, supr);
    fn[proto] = prototype;
    return this;
  };
 
  fn.methods.call(fn, _methods).prototype.constructor = fn;
 
  fn.extend = extend;
  fn.statics = function (spec, optFn) {
    spec = typeof spec === 'string' ? (function () {
      var obj = {};
      obj[spec] = optFn;
      return obj;
    }()) : spec;
    execProcess(this, spec, supr);
    return this;
  };
 
  fn[proto].implement = fn.statics;
 
  return fn;
}
 
function klass(o) {
  return extend.call(isFn(o) ? o : function () {}, o, 1);
}
 
module.exports = klass;