underscore源码剖析

申明
1、源码构造通览,简朴解释申明
2、经由过程挪用要领解说中心代码逻辑

一、源码的构造

为了轻易比对源码,按源码的构造递次展现。
underscore是个轻量级的东西库,大部分代码是完成特定功能以函数的情势存在,自身会比较简朴,没对要领详细申明,可直接参考underscore中文文档

(function() {
  var root = this;
  var previousUnderscore = root._;
  //原型赋值给变量,优点是轻易紧缩
  var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
  //轻易直接挪用原型上的要领
  var
    push             = ArrayProto.push,
    slice            = ArrayProto.slice,
    toString         = ObjProto.toString,
    hasOwnProperty   = ObjProto.hasOwnProperty;
  var
    nativeIsArray      = Array.isArray,
    nativeKeys         = Object.keys,
    nativeBind         = FuncProto.bind,
    nativeCreate       = Object.create;
  var Ctor = function(){};
  //推断obj是不是是_的实例,是返回实例,不是下一步;
  //推断this是不是为_的实例,是把obj赋于this实例的变量_wrapped
  //不是返回一个新实例,实行这个新实例时,新实例内里的this指向该新实例。
  //把obj赋于新实例的变量_wrapped
  var _ = function(obj) {};
  //依据环境将_赋值给差别变量
  if (typeof exports !== 'undefined') {
    if (typeof module !== 'undefined' && module.exports) {
      exports = module.exports = _;
    }
    exports._ = _;
  } else {
    root._ = _;
  }
  _.VERSION = '1.8.3';
  //void 0 是undefined的意义
  //context === void 0返回func;
  //依据argCount的值返回差别的函数
  var optimizeCb = function(func, context, argCount) {};
  //依据value范例返回差别函数
  var cb = function(value, context, argCount) {};
  //挪用cb()
  _.iteratee = function(value, context) {};
  //返回函数func(obj){}
  //挪用func时 arguments长度小于2 或 obj 为空,返回obj
  //拷贝第二个最先今后参数的属性值(undefinedOnly不为真 或 obj无此属性)给obj
  var createAssigner = function(keysFunc, undefinedOnly) {};
  //完成Object.create()
  var baseCreate = function(prototype) {};
  //返回函数func(obj){}
  //挪用func时 obj不为空返回 obj[key]
  var property = function(key) {};
  //2的53次方减1
  var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
  //猎取对象的长度
  var getLength = property('length');
  //经由过程length属性 类数组推断
  var isArrayLike = function(collection) {};

  _.each = _.forEach = function(obj, iteratee, context) {};
  _.map = _.collect = function(obj, iteratee, context) {};

  //返回函数function(obj, iteratee, memo, context);
  //挪用返回函数 为将obj的值以 dir正负代表摆布步数 的递次代入iteratee中(memo有值,以momo为第一个参数),返回终究的效果
  function createReduce(dir) {}

  //从左到右步进1,实行createReduce返回函数
  _.reduce = _.foldl = _.inject = createReduce(1);
  //从右到左步进1,实行createReduce返回函数
  _.reduceRight = _.foldr = createReduce(-1);
  _.find = _.detect = function(obj, predicate, context) {};
  _.filter = _.select = function(obj, predicate, context) {};
  _.reject = function(obj, predicate, context) {};
  _.every = _.all = function(obj, predicate, context) {};
  _.some = _.any = function(obj, predicate, context) {};
  _.contains = _.includes = _.include = function(obj, item, fromIndex, guard) {};
  _.invoke = function(obj, method) {};
  _.pluck = function(obj, key) {};
  _.where = function(obj, attrs) {};
  _.findWhere = function(obj, attrs) {};

  // Infinity为无穷大
  _.max = function(obj, iteratee, context) {};
  _.min = function(obj, iteratee, context) {};
  _.shuffle = function(obj) {};
  _.sample = function(obj, n, guard) {};
  _.sortBy = function(obj, iteratee, context) {};
  //内部要领,返回function(obj, iteratee, context)供groupBy、indexBy、countBy挪用
  var group = function(behavior) {};

  _.groupBy = group(function(result, value, key) {});
  _.indexBy = group(function(result, value, key) {});
  _.countBy = group(function(result, value, key) {});
  _.toArray = function(obj) {};
  _.size = function(obj) {};
  _.partition = function(obj, predicate, context) {};
  _.first = _.head = _.take = function(array, n, guard) {};
  _.initial = function(array, n, guard) {};
  _.last = function(array, n, guard) {};
  _.rest = _.tail = _.drop = function(array, n, guard) {};
  _.compact = function(array) {};
  //内部要领,返回数组供flatten、union、difference、pick挪用
  var flatten = function(input, shallow, strict, startIndex) {};
  _.flatten = function(array, shallow) {};
  _.without = function(array) {};
  _.uniq = _.unique = function(array, isSorted, iteratee, context) {};
  _.union = function() {};
  _.intersection = function(array) {};
  _.difference = function(array) {};
  _.zip = function() {};
  _.unzip = function(array) {};
  _.object = function(list, values) {};
  //内部要领,返回function(obj, iteratee, context)
  function createPredicateIndexFinder(dir) {}
  _.findIndex = createPredicateIndexFinder(1);
  _.findLastIndex = createPredicateIndexFinder(-1);
  _.sortedIndex = function(array, obj, iteratee, context) {};
  function createIndexFinder(dir, predicateFind, sortedIndex) {}
  _.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex);
  _.lastIndexOf = createIndexFinder(-1, _.findLastIndex);
  _.range = function(start, stop, step) {};
  var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {};
  _.bind = function(func, context) {};
  _.partial = function(func) {};
  _.bindAll = function(obj) {};
  _.memoize = function(func, hasher) {};
  _.delay = function(func, wait) {};
  _.defer = _.partial(_.delay, _, 1);
  _.throttle = function(func, wait, options) {};
  };
  _.debounce = function(func, wait, immediate) {};
  _.wrap = function(func, wrapper) {};
  _.negate = function(predicate) {};
  _.compose = function() {};
  _.after = function(times, func) {};
  _.before = function(times, func) {};
  _.once = _.partial(_.before, 2);
  var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
  var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
                      'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];

  function collectNonEnumProps(obj, keys) {}
  _.keys = function(obj) {};
  _.allKeys = function(obj) {};
  _.values = function(obj) {};
  _.mapObject = function(obj, iteratee, context) {};
  _.pairs = function(obj) {};
  _.invert = function(obj) {};
  _.functions = _.methods = function(obj) {};
  _.extend = createAssigner(_.allKeys);
  _.extendOwn = _.assign = createAssigner(_.keys);
  _.findKey = function(obj, predicate, context) {};
  _.pick = function(object, oiteratee, context) {};
  _.omit = function(obj, iteratee, context) {};
  _.defaults = createAssigner(_.allKeys, true);
  _.create = function(prototype, props) {};
  _.clone = function(obj) {};
  _.tap = function(obj, interceptor) {};
  _.isMatch = function(object, attrs) {};
  //+a为parseInt(a);递归推断援用范例
  var eq = function(a, b, aStack, bStack) {};
  _.isEqual = function(a, b) {};
  _.isEmpty = function(obj) {};
  _.isElement = function(obj) {};
  _.isArray = nativeIsArray || function(obj) {};
  _.isObject = function(obj) {};
  //批量增加范例推断的要领
  _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) {
    _['is' + name] = function(obj) {
      return toString.call(obj) === '[object ' + name + ']';
    };
  });
  //arguments.callee当前正在实行函数,严厉形式制止运用
  if (!_.isArguments(arguments)) {}
  //typeof /./为object; typeof Int8Array为function
  if (typeof /./ != 'function' && typeof Int8Array != 'object') {
    _.isFunction = function(obj) {
      return typeof obj == 'function' || false;
    };
  }
  // isFinite(obj)搜检obj是不是是无穷大。
  _.isFinite = function(obj) {};
  _.isNaN = function(obj) {};
  _.isBoolean = function(obj) {};
  _.isNull = function(obj) {};
  _.isUndefined = function(obj) {};
  _.has = function(obj, key) {};
  //原root._指向_;root._ = previousUnderscore将root._指向页面原有的_;
  //this指向_
  _.noConflict = function() {};
  _.identity = function(value) {};
  _.constant = function(value) {};
  _.noop = function(){};
  _.property = property;
  _.propertyOf = function(obj) {};
  _.matcher = _.matches = function(attrs) { };
  _.times = function(n, iteratee, context) {};
  _.random = function(min, max) {};
  _.now = Date.now || function() {};
  var escapeMap = {
    '&': '&',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#x27;',
    '`': '&#x60;'
  };
  var unescapeMap = _.invert(escapeMap);
  var createEscaper = function(map) {};
  _.escape = createEscaper(escapeMap);
  _.unescape = createEscaper(unescapeMap);
  _.result = function(object, property, fallback) {};
  var idCounter = 0;
  _.uniqueId = function(prefix) {};
  //模板相干
  _.templateSettings = {
    evaluate    : /<%([\s\S]+?)%>/g,
    interpolate : /<%=([\s\S]+?)%>/g,
    escape      : /<%-([\s\S]+?)%>/g
  };
  var noMatch = /(.)^/;
  var escapes = {
    "'":      "'",
    '\\':     '\\',
    '\r':     'r',
    '\n':     'n',
    '\u2028': 'u2028',
    '\u2029': 'u2029'
  };
  var escaper = /\\|'|\r|\n|\u2028|\u2029/g;
  var escapeChar = function(match) {
    return '\\' + escapes[match];
  };
  _.template = function(text, settings, oldSettings) {};

  //链式挪用
  _.chain = function(obj) {};
  // 中心函数,是不是链式挪用
  var result = function(instance, obj) {
    return instance._chain ? _(obj).chain() : obj;
  };
  // 将obj上面的函数表达式悉数挂载到_的原型上
  _.mixin = function(obj) {};
  //将_上面的函数表达式悉数挂载到_的原型上
  _.mixin(_);

  // 将数组要领挂载到_的原型上(原数组转变)
  _.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
    var method = ArrayProto[name];
    _.prototype[name] = function() {
      var obj = this._wrapped;
      method.apply(obj, arguments);
      if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
      return result(this, obj);
    };
  });

  //将数组要领挂载到_的原型上(原数组稳定)
  _.each(['concat', 'join', 'slice'], function(name) {
    var method = ArrayProto[name];
    _.prototype[name] = function() {
      return result(this, method.apply(this._wrapped, arguments));
    };
  });
  _.prototype.value = function() {};
  _.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
  _.prototype.toString = function() {};
  if (typeof define === 'function' && define.amd) {
    define('underscore', [], function() {
      return _;
    });
  }
}.call(this));

二、两种挪用要领

1、最经常使用的挪用体式格局_.method(arguments)
示例1:

var arr = [1, 2, 3];
// 最经常使用,类函数式编程
var test1=_.map(arr, function(num) {
    return num;
});

挪用流程重要代码:

var root = this;
//_函数表达式声明
var _ = function(obj) {
    if (obj instanceof _) return obj;
    if (!(this instanceof _)) return new _(obj);
    this._wrapped = obj;
  };
//将_函数赋值给挪用的exports._或 root._(依据环境)
if (typeof exports !== 'undefined') {
    if (typeof module !== 'undefined' && module.exports) {
      exports = module.exports = _;
    }
    exports._ = _;
  } else {
    root._ = _;
  }
//声明差别的要领作为_的属性值
  _.map = _.collect = function(obj, iteratee, context) {
    iteratee = cb(iteratee, context);
    var keys = !isArrayLike(obj) && _.keys(obj),
        length = (keys || obj).length,
        results = Array(length);
    for (var index = 0; index < length; index++) {
      var currentKey = keys ? keys[index] : index;
      results[index] = iteratee(obj[currentKey], currentKey, obj);
    }
    return results;
  };

经由过程以上的代码,可直接挪用_.map函数。此要领比较简朴易懂

2、OOP作风,类jquery挪用_(arguments1).method(arguments2)
示例2:

var arr = [1, 2, 3];
// OOP作风,类jquery挪用
var test2=_(arr).map(function(num) {
    return num* 3;
});

示例3:链式挪用

var stooges = [{name: 'curly', age: 25}, {name: 'moe', age: 21}, {name: 'larry', age: 23}];
var test3 = _.chain(stooges)
    .sortBy(function(stooge){ return stooge.age; })
    .map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
    .first()
    .value();
console.log("test3");
console.log(test3);

挪用流程重要代码:

var _ = function(obj) {
    //推断obj是不是是_的实例,是返回实例,不是下一步;
    if (obj instanceof _) return obj;
    //推断this是不是为_的实例,是把obj赋于this实例的变量_wrapped
     //不是返回一个新实例,实行这个新实例时,新实例内里的this指向该新实例。
    if (!(this instanceof _)) return new _(obj);
    //把obj赋于新实例的变量_wrapped
    this._wrapped = obj;
  };


_.chain = function(obj) {
    //推断obj是不是是_的实例,是返回该实例,不是天生一个新实例;将实例赋值给instance 
    var instance = _(obj);
    //链式挪用标记
    instance._chain = true;
    //返回实例
    return instance;
  };

var result = function(instance, obj) {
    //是不是继承链式挪用
    return instance._chain ? _(obj).chain() : obj;
  };

_.mixin = function(obj) {
    //_.functions(obj)返回obj上值范例为function的key的鸠合
    _.each(_.functions(obj), function(name) {
      var func = _[name] = obj[name];
      _.prototype[name] = function() {
        //如示例2,this._wrapped为arr
        var args = [this._wrapped];
         //如示例2,args变成[arr,function(num) {return num* 3;}]
        push.apply(args, arguments);
        //如示例2,返回_.map.apply(_,[arr,function(num) {return num* 3;}])
        return result(this, func.apply(_, args));
      };
    });
  };

//将_传入_.mixin
_.mixin(_);

//猎取链式挪用的效果
_.prototype.value = function() {
   return this._wrapped; 
};
    原文作者:kalakalaxyz
    原文地址: https://segmentfault.com/a/1190000013288540
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞