Skip to content

工具方法系列

常见方法

debounce 函数防抖

函数防抖

作用: 一个函数在一段时间内多次触发都只执行最后一次
原理: 利用定时器,在函数第一次执行时设定一个定时器,再次调用时如果已经设定过定时器就清空之前的定时器并设定一个新的定时器,当定时器结束后执行传入的回调函数
应用: 搜索输入框获取用户输入的联想结果

实现防抖函数
js
function debounce(fn, wait) {
  // 通过闭包缓存定时器 id
  let timer = null
  return function (...args) {
    // 如果定时器已经存在,清除定时器
    if (timer) {
      clearTimeout(timer)
      timer = null
    }
    // 设定定时器,定时器结束后执行传入的回调函数 fn
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, wait)
  }
}

throttle 函数节流

函数节流

作用: 函数节流指指的是在一段时间内只允许函数执行一次 (例如 3 秒执行一次那么在函数第一次调用后的 3 秒内后面的函数调用将被忽略)
原理: 利用时间戳来判断,记录上次执行的时间戳,在每次触发事件时判断当前时间是否大于上次执行的时间 + 设置的间隔 ,如果是则执行回调并更新上次执行的时间戳
应用: 降低 scroll resize 事件的触发频率

实现节流函数
js
function throttle(fn, wait) {
  // 通过闭包缓存上一次的调用时间 (默认为 0)
  let lastCallTime = 0
  return function () {
    const now = Date.now()
    // 判断当前调用时间和上次调用时间的差值是否大于 wait
    if (now - lastCallTime >= wait) {
      // 更新调用时间
      lastCallTime = now
      // 执行回调函数
      fn.apply(this, arguments)
    }
  }
}

deepClone 深拷贝

深拷贝

原理: 利用 JSON.parse(JSON.stringify(obj)) 实现深拷贝,但是 JSON.stringify 只能处理对象和数组,不能处理函数、undefined、symbol 等类型,所以需要递归处理。 使用 WeakMap 的好处是,WeakMap 存储的 key 必须是对象,并且 key 都是弱引用,便于垃圾回收

应用: 拷贝对象、数组等复杂数据类型,避免引用类型的数据被修改

实现深拷贝
js
/**
 * 深度克隆一个对象或数组
 */
function deepClone(target, hash = new WeakMap()) {
  // 检查目标是否为对象,若不是则直接返回
  if (!isObject(target)) return target
  // 检查hash中是否已存在目标对象,若存在则返回该对象以解决循环引用问题
  if (hash.has(target)) return hash.get(target)
  let obj

  // 处理特殊对象类型
  if (target instanceof Date) {
    obj = new Date(target)
  } else if (target instanceof RegExp) {
    obj = new RegExp(target)
  } else if (target instanceof Map) {
    obj = new Map()
    target.forEach((value, key) => {
      obj.set(key, deepClone(value, hash))
    })
  } else if (target instanceof Set) {
    obj = new Set()
    target.forEach((value) => {
      obj.add(deepClone(value, hash))
    })
  } else {
    // 根据目标对象是数组还是对象,创建相应的空数组或对象
    obj = Array.isArray(target) ? [] : {}
  }
  // 不考虑那么多特殊类型的话上方这一段改成 obj = Array.isArray(target) ? [] : {}

  // 将新创建的对象存入hash,键为目标对象,值为新创建的对象,以处理循环引用
  hash.set(target, obj)
  // 遍历目标对象的所有属性
  for (let key in target) {
    // 确保只处理目标对象自身的属性,而非原型链上的属性
    if (target.hasOwnProperty(key)) {
      // 如果当前属性值是对象,则递归调用deepClone进行深度克隆
      if (isObject(target[key])) {
        obj[key] = deepClone(target[key], hash)
      } else {
        // 如果当前属性值不是对象,则直接赋值
        obj[key] = target[key]
      }
    }
  }
  // 返回克隆后的对象
  return obj
}

const isObject = (target) => {
  return typeof target === 'object' && target !== null
}

const obj = {
  a: 2,
  b: 3,
  c: { d: 4 },
}

obj.d = obj

const newObj = deepClone(obj)
console.log(newObj)

快排算法

快排算法

原理: 快排算法的原理是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另一部分小,然后再对这两部分分别进行排序,这样整个数据就变成有序的了。

应用: 快速排序算法,用于对数组进行排序

实现快排
js
function quickSort(arr) {
  if (arr.length <= 1) {
    return arr
  }
  let mid = arr[0]
  const left = arr.slice(1).filter((item) => item < mid)
  const right = arr.slice(1).filter((item) => item >= mid)
  return [...quickSort(left), mid, ...quickSort(right)]
}

console.log(quickSort([5, 3, 8, 4, 2])) // [2, 3, 4, 5, 8]

柯里化函数

柯里化函数

原理:柯里化函数,也叫部分求值,是指将接受多个参数的函数,变换成接受一个或多个参数的函数。

实现柯里化函数
js
function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn(...args)
    } else {
      return (...rest) => curried(...args, ...rest)
    }
  }
}

// 示例
const sum = (a, b, c) => a + b + c
const csum = curry(sum)
const res = csum(1)(2)(3) // 6
console.log(res)

数组转树

数组转树

原理:数组转树,是指将一个数组转换成树结构。 应用: 树结构,如目录结构,菜单结构,树形结构等等

实现数组转树
js
function listToTree(list) {
  const map = {},
    root = []
  list.forEach((item) => {
    map[item.id] = {
      ...item,
      children: [],
    }

    if (item.parentId === null) {
      root.push(map[item.id])
    }
    if (map[item.parentId]) {
      map[item.parentId].children.push(map[item.id])
    }
  })
  return root
}

const arr = [
  { id: 1, parentId: null },
  { id: 2, parentId: 1 },
  { id: 3, parentId: 1 },
  { id: 4, parentId: 2 },
]
console.log(JSON.stringify(listToTree(arr)))

树转数组

树转数组

原理:树转数组,是指将一个树结构转换成数组。 应用: 树结构,如目录结构,菜单结构,树形结构等等

实现树转数组
js

Released under the MIT License.