We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
裁剪数组array,从start位置开始到end结束,但不包括end本身的位置。也就是左闭右开的形式。 这个方法可以替代数组本身的Array.slice()方法来确保数组正确返回。 【参数】
start和end都传
start和end都不传
只传start
源码: 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; // 这里的>>>表示向右移位,>>>0的意思是无符号移位,该操作符会将第一个操作数向右移动指定的位数。向右被移出的位被 // 丢弃,左侧用0填充。因为符号位变成了0,所以结果总是非负的。 start >>>= 0; let index = -1; const result = new Array(length); while (++index < length) { result[index] = array[index + start]; // 当index + 1的值小于数组长度时,result数组的index位置的值被赋值为array的[index+start位置的值] } return result; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
1. 作用及使用
裁剪数组array,从start位置开始到end结束,但不包括end本身的位置。也就是左闭右开的形式。
这个方法可以替代数组本身的Array.slice()方法来确保数组正确返回。
【参数】
2. 示例
start和end都传
start和end都不传
只传start
3. 源码分析
The text was updated successfully, but these errors were encountered: