JS手撕,经典面试题

7,338 阅读4分钟

引言

首先出这篇文章,一方面是为了记录巩固我所学的知识,明白面试的高频考点。不鼓励大家背题的,初衷是希望总结的一些面试题能帮助你查漏补缺,温故知新。这些题并不是全部,如果你还想看得更多,可以访问GitHub仓库,目前已经有552道大厂真题了,涵盖各类前端的真题,欢迎加入我们一起来讨论~

函数

call

  • 语法:fn.call(obj,...args)
  • 功能:执行fn,使this为obj,并将后面的n个参数传给fn
Function.prototype.myCall = function (obj, ...args) {
  if (obj == undefined || obj == null) {
    obj = globalThis
  }
  obj.fn = this
  let res = obj.fn(...args)
  delete obj.fn
  return res
}
value = 2

let foo = {
  value: 1,
}

let bar = function (name, age) {
  console.log(name, age, this.value)
}

bar.myCall(foo, 'HearLing', 18) //HearLing 18 1
bar.myCall(null, 'HearLing', 18) //HearLing 18 2

apply

  • 语法:fn.apply(obj,arr)
  • 功能:执行fn,使this为obj,并arr数组中元素传给fn
Function.prototype.myAplly = function (obj, arr) {
  if (obj == undefined || obj == null) {
    obj = globalThis
  }
  obj.fn = this
  let res = obj.fn(...arr)
  delete obj.fn
  return res
}
value = 2

let foo = {
  value: 1,
}

let bar = function (name, age) {
  console.log(name, age, this.value)
}

bar.myAplly(foo, ['HearLing', 18]) //HearLing 18 1
bar.myAplly(null, ['HearLing', 18]) //HearLing 18 2

bind

  • 语法:fn.bind(obj,...args)
  • 功能:返回一个新函数,给fn绑定this为obj,并制定参数为后面的n个参数
Function.prototype.myBind = function (obj, ...args) {
  let that = this
  let fn = function () {
    if (this instanceof fn) {
      return new that(...args)
    } else {
      return that.call(obj, ...args)
    }
  }
  return fn
}

value = 2

let foo = {
  value: 1,
}

let bar = function (name, age) {
  console.log(name, age, this.value)
}
let fn = bar.myBind(foo, 'HearLing', 18)
//fn() //HearLing 18 1
let a = new fn() //HearLing 18 undefined
console.log(a.__proto__)//bar {}

区别call()/apply()/bind()

call(obj)/apply(obj)::调用函数, 指定函数中的this为第一个参数的值 bind(obj): 返回一个新的函数, 新函数内部会调用原来的函数, 且this为bind()指定的第一参数的值

节流

  • 理解:在函数多次频繁触发时,函数执行一次后,只有大于设定的执行周期后才会执行第二次
  • 场景:页面滚动(scroll)、DOM 元素的拖拽(mousemove)、抢购点击(click)、播放事件算进度信息
  • 功能:节流函数在设置的delay毫秒内最多执行一次(简单点说就是,我上个锁,不管你点了多少下,时间到了我才解锁)
function throttle(fn, delay) {
  let flag = true
  return (...args) => {
    if (!flag) return
    flag = false
    setTimeout(() => {
      fn.apply(this, args)
      flag = true
    }, delay)
  }
}

防抖

  • 理解:在函数频繁触发是,在规定之间以内,只让最后一次生效
  • 场景:搜索框实时联想(keyup/input)、按钮点击太快,多次请求(登录、发短信)、窗口调整(resize)
  • 功能:防抖函数在被调用后,延迟delay毫秒后调用,没到delay时间,你又点了,清空计时器重新计时,直到真的等了delay这么多秒。
function debounce(fn, delay) {
  let timer = null
  return (...args) => {
    clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}

节流与防抖的区别

首先概念上的不同,解释一下什么是防抖节流;然后就是使用场景的不同; 经典区分图:

curry

function mycurry(fn, beforeRoundArg = []) {
  return function () {
    let args = [...beforeRoundArg, ...arguments]
    if (args.length < fn.length) {
      return mycurry.call(this, fn, args)
    } else {
      return fn.apply(this, args)
    }
  }
}

function sum(a, b, c) {
  return a + b + c
}

let sumFn = mycurry(sum)
console.log(sumFn(1)(2)(3))//6

数组

数组去重

function unique(arr) {
  const res = []
  const obj = {}
  arr.forEach((item) => {
    if (obj[item] === undefined) {
      obj[item] = true
      res.push(item)
    }
  })
  return res
}
//其他方法
//Array.from(new Set(array))
//[...new Set(array)]

数组扁平化

// 递归展开
function flattern1(arr) {
  let res = []
  arr.foreach((item) => {
    if (Array.isArray(item)) {
      res.push(...flattern1(item))
    } else {
      res.push(item)
    }
  })
  return res
}

对象

new

function newInstance (Fn, ...args) {
  const obj = {}
  obj.__proto__ = Fn.prototype
  const result = Fn.call(obj, ...args)
  // 如果Fn返回的是一个对象类型, 那返回的就不再是obj, 而是Fn返回的对象否则返回obj
  return result instanceof Object ? result : obj
}

instanceof

function instance_of(left, right) {
  let prototype = right.prototype
  while (true) {
    if (left === null) {
      return false
    } else if (left.__proto__ === prototype) {
      return true
    }
    left = left.__proto__
  }
}
let a = {}
console.log(instance_of(a, Object))//true

对象数组拷贝

浅拷贝

// 浅拷贝的方法
//Object.assign(target,...arr)
// [...arr]
// Array.prototype.slice()
// Array.prototype.concate()

function cloneShallow(origin) {
  let target = {}
  for (let key in origin) {
    if (origin.hasOwnProperty(key)) {
      target[key] = origin[key]
    }
  }
  return target
}
let obj = {
  name: 'lala',
  skill: {
    js: 1,
    css: 2,
  },
}
let newobj = cloneShallow(obj)
newobj.name = 'zhl'
newobj.skill.js = 99
console.log(obj)//{ name: 'lala', skill: { js: 99, css: 2 } }
console.log(newobj)//{ name: 'zhl', skill: { js: 99, css: 2 } }

深拷贝

// 浅拷贝的方法
//JSON.parse(JSON.stringify(obj))
function deepClone(target,hashMap=new WeakMap()){
    if(typeof target !== 'object' ||  target==null ){
        if(target instanceof Function) return target.call(this,...arguments)
        return target
    }
    if(target instanceof Date)return new Date(target);
    if(target instanceof RegExp)return new RegExp(target);
    let res = new target.constructor()
    if(hashMap.get(target))return hashMap.get(target)
    hashMap.set(res,target)
    for(let key in target){
        res[key] = deepClone(deepClone(target[key],hashMap))
    }
    return res
}

const a = {
    i: Infinity,
    s: "",
    bool: false,
    n: null,
    u: undefined,
    sym: Symbol(),
    obj: {
      i: Infinity,
      s: "",
      bool: false,
      n: null,
      u: undefined,
      sym: Symbol(),
    },
    array: [
      {
        nan: NaN,
        i: Infinity,
        s: "",
        bool: false,
        n: null,
        u: undefined,
        sym: Symbol(),
      },
      123,
    ],
    fn: function () {
      return "fn";
    },
    date: new Date(),
    re: /hi\d/gi,
  };
  let a2 = deepClone(a);
  console.log(a2 !== a);
  console.log(a2.i === a.i);
  console.log(a2.s === a.s);
  console.log(a2.bool === a.bool);
  console.log(a2.n === a.n);
  console.log(a2.u === a.u);
  console.log(a2.sym === a.sym);
  console.log(a2.obj !== a.obj);
  console.log(a2.array !== a.array);
  console.log(a2.array[0] !== a.array[0]);
  console.log(a2.array[0].i === a.array[0].i);
  console.log(a2.array[0].s === a.array[0].s);
  console.log(a2.array[0].bool === a.array[0].bool);
  console.log(a2.array[0].n === a.array[0].n);
  console.log(a2.array[0].u === a.array[0].u);
  console.log(a2.array[0].sym === a.array[0].sym);
  console.log(a2.array[1] === a.array[1]);
  console.log(a2.fn !== a.fn);
  console.log(a2.date !== a.date);
  console.log(a2.re !== a.re);
  // 都要为 true

最后的话

🚀🚀 更多基础知识总结可以⭐️关注我,后续会持续更新面试题总结~

⭐️⭐️ 最后祝各位正在准备秋招补招和春招的小伙伴面试顺利~,收割offer,我们一起加油吧🤝!还有就是快春节了,提前祝你新年快乐~❤ ❤