Skip to content

Latest commit

 

History

History
82 lines (63 loc) · 2.05 KB

2017-11-28__lodash.md

File metadata and controls

82 lines (63 loc) · 2.05 KB

#笔记

lodash阅读

Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1

按位操作符

  • 按位移动会先将操作数转换为大端 (big-endian) 表示的 32位整数
  • 并返回与左操作数相同类型的结果
  • 右操作数应当小于 32,超出会被丢弃

有符号右移(>>)和无符号右移(>>>)的区别

  • 例如, 9 >> 2 得到 2
9 (base 10): 00000000000000000000000000001001 (base 2)
                  --------------------------------
9 >> 2 (base 10): 00000000000000000000000000000010 (base 2) = 2 (base 10)
  • 相比之下, -9 >> 2 得到 -3,因为符号被保留了。
-9 (base 10): 11111111111111111111111111110111 (base 2)
                   --------------------------------
-9 >> 2 (base 10): 11111111111111111111111111111101 (base 2) = -3 (base 10)

lodash.slice

start >>>= 0; // 能保证start的值不会超过Math.pow(2, 32) - 1

lodash.eq

function eq(value, other) {
  return value === other || (value !== value && other !== other); // 保证了eq(NaN, NaN)能返回true
}

lodash.isLength(用于赋值判断array-like)

/*
*  @param {*} value The value to check.
*  @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
*/
function isLength(value) {
  return typeof value == 'number' &&
    value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER; // value % 1 == 0 能判断是否为整数
}

lodash.isObject

function isObject(value) {
  var type = typeof value;
  return !!value && (type == 'object' || type == 'function'); // typeof null === 'object'
}

lodash.isFunction

function isFunction(value) {
  // The use of `Object#toString` avoids issues with the `typeof` operator
  // in Safari 8-9 which returns 'object' for typed array and other constructors.
  var tag = isObject(value) ? objectToString.call(value) : '';
  return tag == '[object Function]' || tag == '[object GeneratorFunction]';
}

lodash.isArrayLike

function isArrayLike(value) {
  return value != null && isLength(value.length) && !isFunction(value);
}