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)) {}
这个地方十分的不解,求大神解答,感谢!
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你可能看了假文档,真文档在这