Why don’t you use native function in reduceRight of understanding core?

  node.js, question

readingunderscoreThe source code, there is a question, why not give priority to the originalreduceAndreduceRight

Source code, such as_.keys, is to detect whether support firstObject.keysYes.

// Create a reducing function iterating left or right.
  // ? why dont use native reduce
  var createReduce = function(dir) {
    // Wrap code that reassigns argument variables in a separate function than
    // the one that accesses `arguments.length` to avoid a perf hit. (#1991)
    var reducer = function(obj, iteratee, memo, initial) {
      var keys = !isArrayLike(obj) && _.keys(obj),
          length = (keys || obj).length,
          index = dir > 0 ? 0 : length - 1;
      if (!initial) {
        memo = obj[keys ? keys[index] : index];
        index 加= dir;
      }
      for (; index >= 0 && index < length; index 加= dir) {
        var currentKey = keys ? keys[index] : index;
        memo = iteratee(memo, obj[currentKey], currentKey, obj);
      }
      return memo;
    };

    return function(obj, iteratee, memo, context) {
      var initial = arguments.length >= 3;
      return reducer(obj, optimizeCb(iteratee, context, 4), memo, initial);
    };
  };
// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`.
_.reduce = _.foldl = _.inject = createReduce(1);

// The right-associative version of reduce, also known as `foldr`.
_.reduceRight = _.foldr = createReduce(-1);

Native reduce is only used for arrays, while underscore’s reduce can be used for Object

By the way, Amway my Repo->https://github.com/hanzichi/underscore-analysis