lodash源码学习

1. arrayEach
/**
   * A specialized version of `_.forEach` for arrays without support for iteratee shorthands.
   * @private
   * @param {Array} [array] The array to iterate over.
   * @param {Function} iteratee The function invoked per iteration.          
   * @returns {Array} Returns `array`.                                       
   */
  function arrayEach(array, iteratee) {
    var index = -1,
        length = array == null ? 0 : array.length;

    while (++index < length) {
      if (iteratee(array[index], index, array) === false) {  // 回调返回false则中断循环
        break;
      }
    }
    return array;
  }
2. arrayEachRight
/**
   * A specialized version of `_.forEachRight` for arrays without support for iteratee shorthands.
   * @private
   * @param {Array} [array] The array to iterate over.
   * @param {Function} iteratee The function invoked per iteration.
   * @returns {Array} Returns `array`.
   */
  function arrayEachRight(array, iteratee) {
    var length = array == null ? 0 : array.length;

    while (length--) {      // 递减,while(0)会停止
   
if (iteratee(array[length], length, array) === false) { break; } } return array; }

猜你喜欢

转载自www.cnblogs.com/hcxy/p/9700689.html
今日推荐