【JavaScript】各种常见手写题汇总复习

防抖

function throttle(fun, time) {
  let timer = null
  return () => {
    if (timer) {
      return
    }
    fun()
    timer = setTimeout(() => {
      timer = null
    }, time)
  }
}

节流

function debounce(fun, time) {
  let timer = null
  return () => {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fun()
    }, time)
  }
}

深拷贝

function deepClone(a, cache) {
  let res = undefined

  if (!cache) {
    cache = new Map()
  }

  if (a instanceof Object) {
    if (cache.get(a)) {
      return cache.get(a)
    }

    if (a instanceof Function) {
      if (a.prototype) {
        res = (...args) => {
          return a.call(this, ...args)
        }
      } else {
        res = (...args) => {
          return a.call(undefined, ...args)
        }
      }
    } else if (a instanceof Array) {
      res = []
    } else if (a instanceof Date) {
      res = new Date(a - 0)
    } else if (a instanceof RegExp) {
      res = new RegExp(a)
    } else {
      res = {}
    }

    cache.set(a, res)

    for (let k in a) {
      if (a.hasOwonProperty(k)) {
        res[k] = deepClone(a[k])
      }
    }

    return res
  } else {
    res = a
  }
}

手写 instanceof

function _instanceof(a, b) {
  if (typeof a !== 'object' || a === null) {
    return false
  }

  let proto = Object.getPrototypeOf(a)
  while (true) {
    if (proto === null) {
      return false
    }
    if (proto === b.prototype) {
      return true
    }
    proto = Object.getPrototypeOf(proto)
  }
}

手写 new

function _new(fn, ...args) {
  let o = Object.create(fn.prototype)
  let res = fn.apply(o, args)
  return typeof res === 'object' ? res : o
}

数组去重

// 当前项和剩余项比较
function listDoWeight(arr) {
  let res = []
  for (let i = 0; i < arr.length; i++) {
    let c = arr[i]
    let o = arr.slice(i + 1)
    if (o.indexOf(c) === -1) {
      res.push(c)
    }
  }
  return res
}

// 先排序,再遍历及相邻元素对比去重
function listDoWeight(arr) {
  arr.sort((a, b) => a - b)
  let res = [arr[0]]
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] !== arr[i - 1]) {
      res.push(arr[i])
    }
  }
  return res
}

// set
function listDoWeight(arr) {
  return Array.from(new Set(arr))
}

// map
function listDoWeight(arr) {
  const map = new Map()

  for (let i = 0; i < arr.length; i++) {
    let temp = arr[i]
    if (!temp) {
      continue
    }
    if (map.has(temp)) {
      continue
    }
    map.set(temp, true)
  }
  return [...map.keys]
}

数组扁平化

let arr = [1, [2, [3, [4, 5]]], 6]
let res
// flat 方法
res = arr.flat(arr)

// 递归
function f1(arr) {
  let res = []
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      res = res.concat(flatFun(arr[i]))
    } else {
      res.push(arr[i])
    }
  }
  return res
}

// reduce 迭代
function f2(arr) {
  return arr.reduce((prev, next) => {
    return prev.concat(Array.isArray(next) ? f2(next) : next)
  }, [])
}

// 扩展运算符
function f3(arr) {
  while (arr.some((i) => Array.isArray(i))) {
    arr = [].concat(...arr)
  }
  return arr
}
posted @ 2022-08-22 11:15  努力挣钱的小鑫  阅读(34)  评论(0编辑  收藏  举报