前端面试常见手写题目

1.实现防抖

function debounce(fn,delay = 200){
  if(typeof fn !== 'function'){
    throw new TypeError('fn is not a function')
  }
  let lastFn = null;
  return function(...args){
    if(lastFn) clearTimeout(lastFn)
    lastFn = setTimeout(()=>{
      fn.call(this,...args)
    },delay)
  }
}

function clickHandle(){
  console.log('test')
}

window.onscroll = debounce(clickHandle,500)

2.实现接节流

function throttle(fn,delay = 200){
  if(typeof fn !== 'function'){
    throw new TypeError('fn is not a function')
  }
  let flag = null;
  return function(...args){
    if(!flag){
      setTimeout(()=>{
        flag = true
        fn.call(this,...args)
      },delay)
    }
  }
}
function clickHandle(){
  console.log('test')
}

window.onscroll = debounce(clickHandle,500)

3.实现bind

Function.prototype.bind = function(obj,arg){
  let arg = Array.prototype.slice.call(arguments,1)
  let _this = this
  let obj = function(newArg){
    arg = arg.concat( Array.prototype.slice.call(newArg))
    return content.apply(obj,arg)
  }
  obj.prototype = Object.create(_this.prototype)
  return obj
}

4.new内部原理(new Parent())

function myNew(){
  let obj = {}
  obj.__proto__ = Parent.prototype
  Parent.call(obj)
  return obj
}

5.函数柯里化

  • 这里运行出来是一个Function,所以没有达到实际的效果
function add(){
  let args = Array.from(arguments)
  let _adder = function(){
    args.push(...arguments)
    return _adder
  }

  _adder.toString = function(){
     return _args.reduce(function (a, b) {
            return a + b;
        });
  }
  return _adder
}
console.log(add(1,2)(2))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值