lodash源码baseSlice中的 start >>> 0 作用是什么呢?

/**
 * The base implementation of `_.slice` without an iteratee call guard.
 *
 * @private
 * @param {Array} array The array to slice.
 * @param {number} [start=0] The start position.
 * @param {number} [end=array.length] The end position.
 * @returns {Array} Returns the slice of `array`.
 */
function baseSlice(array, start, end) {
  var 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; // ???

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

export default baseSlice;

移动0位不还是本身么?

阅读 1.6k
1 个回答

自问自答,刚刚查到了

1 >>> 0 // 1
16 >>> 0 // 16
-1 >>> 0 // 4294967295
Function >>> 0 // 0
let a = {}
a >>> 0 // 0
null >>> 0 // 0

无符号位运算符,能够将非数字转为0,负数转为正数
说白了就是确保这个变量是一个有意义的数字。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题