javascript - 跪求大神解答关于lodash中dropWhile方法源码的疑惑
巴扎黑
巴扎黑 2017-04-11 11:55:25
[JavaScript讨论组]

dropWhile/dropRightWhile 中第二个参数 支持Function,String,Object, 文档也有说当传入不同类型的时候,会用相应的方法处理,但是我看源码,没看到这个是在哪里做处理的,以dropWhile为例:

dropWhile.js

import baseWhile from './.internal/baseWhile.js';
function dropWhile(array, predicate) {
  return (array && array.length)
    ? baseWhile(array, predicate, true)
    : [];
}
export default dropWhile;

baseWhile.js

import baseSlice from './baseSlice.js';
function baseWhile(array, predicate, isDrop, fromRight) {
  const length = array.length;
  let index = fromRight ? length : -1;

  while ((fromRight ? index-- : ++index < length) &&
    predicate(array[index], index, array)) {} 

  return isDrop
    ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
    : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index));
}

export default baseWhile;

baseSlice.js

function baseSlice(array, start, end) {
  let index = -1,
      length = array.length

  if (start < 0) {
    start = -start > length ? 0 : (length + start)
  }
  end = end > length ? length : end
  if (end < 0) {
    end += length
  }
  length = start > end ? 0 : ((end - start) >>> 0)
  start >>>= 0

  const result = Array(length)
  while (++index < length) {
    result[index] = array[index + start]
  }
  return result
}

export default baseSlice

从这几个部分的源代码上看,并没有对第二个参数(predicate)进行任何的判断和处理就直接在baseWhile.js 进行运用,如果出入是String或Object类型的怎么去调用文档所说的_.matches
_.matchesProperty_.property方法?

  while ((fromRight ? index-- : ++index < length) &&
    predicate(array[index], index, array)) {} 

这个地方十分的不解,求大神解答,感谢!

巴扎黑
巴扎黑

全部回复(1)
大家讲道理

你可能看了假文档,真文档在这

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号