拿来-util工具函数

记录一些写的好的工具函数。以便学习和项目中直接拿来使用。

  • 判断值是否相等:使用于任何数据类型:基本数据类型和复杂深层次对象
function deepEqual (a, b) {
  if (a === b) return true

  if (a instanceof Date && b instanceof Date) {
    // If the values are Date, they were convert to timestamp with getTime and compare it
    if (a.getTime() !== b.getTime()) return false
  }

  if (a !== Object(a) || b !== Object(b)) {
    // If the values aren't objects, they were already checked for equality
    return false
  }

  const props = Object.keys(a)

  if (props.length !== Object.keys(b).length) {
    // Different number of props, don't bother to check
    return false
  }

  return props.every(p => deepEqual(a[p], b[p]))
}
原文地址:https://www.cnblogs.com/damonFeng/p/9995509.html