Array - 源码阅读 · lodash

_.slice()

和原生 Array.slice 的区别
  1. 返回的是一个密集数组,原生的有可能会返回一个稀疏数组
  2. 稀疏数组在迭代器里不会执行
源码细节
  1. 使用 >>> 0 无符号右移运算符处理有可能的异常情况,将非整数变成一个整数
  2. 然后使用 while 循环做了一次浅拷贝
源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function slice(array, start, end) {
let length = array == null ? 0 : array.length;
if (!length) {
return [];
}
start = start == null ? 0 : start;
end = end === undefined ? length : end;

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;

let index = -1;
const result = new Array(length);
while (++index < length) {
result[index] = array[index + start];
}
return result;
}

HoHo !


Array - 源码阅读 · lodash
https://wanmeishijie.xyz/notes/code-reading/lodash/Array/
作者
发布于
2023年11月25日
许可协议