【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
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章