前端面试常见手写代码集合

本文汇总了前端面试中常见的手写代码题目,包括节流、防抖、改变this指向的方法、Promise原理与实践、深拷贝、发布订阅模式、new操作符解析、Object.create实现、Ajax基础、JSONP、路由、函数柯里化、instanceof用法、数组操作和异步控制等核心知识点。详细介绍了每个概念并提供了相关实践。
摘要由CSDN通过智能技术生成


1、节流和防抖

节流

在 s 秒内即使被触发多次,也只能执行一次。

function throttle(fn, delay){
   
  let flag = true;
  return function (...args){
   
    if(!flag){
   
      return;
    }
    flag = false;
    setTimeout(() => {
   
      fn.apply(this, args);
      flag = true;
    }, delay)
  }
}

防抖

s 秒内函数只会被执行一次,如果 s 秒内多次被触发,则以最后一次触发为标准重新计算延迟时间。

function debounce(fn, delay){
   
  let timer;
  return function(...args){
   
    clearTimeout(timer);
    timer = setTimeout(() => {
   
      fn.apply(this, args);
    }, delay)
  }
}

2、改变this指向的函数

bind

Function.prototype.bind = function (context){
   
  context = context || window;
  context.fn = this;
  let args = [... arguments].slice(1);
  return function func() {
   
    // 判断是否被当做构造函数使用
    if(this instanceof func) {
   
      return new context.fn(...args, ...arguments);
    }
    return context.fn.apply(context,args.concat(...arguments));
  }
}

apply

Function.prototype.call = function (context){
   
  context = context || window;
  context.fn = this;
  let result;
  let args = [...arguments].splice(1);
  if(arguments[1]){
   
    result = context.fn(...args);
  } else {
   
    result = context.fn();
  }
  delete context.fn;
  return result;
}

call

Function.prototype.apply = function (context){
   
  context = context || window;
  context.fn = this;
  let args = [...arguments].splice(1);
  let result = context.fn(...args);
  delete context.fn;
  return result;
}

3、promise

概念

promise 对象是一个代理对象,被代理的值在 promise 对象创建时可能是未知的,可以为异步操作的成功和失败分别绑定相应的处理方法。 让异步方法可以像同步方法那样返回值,但并不是立即返回最终执行结果,而是一个能代表未来出现的结果的promise对象。解决了异步回调地狱问题。

状态

1、pending:进行中
2、fulfilled:已成功
3、rejected:失败
只有异步操作才能决定当前的状态,其他操作无法改变。
一旦状态确定,就不会再改变,这时候称为定型 resolved,状态变化情况:pending ——> fulfilled,pending ——> rejected。

实现一个promise

class Promise{
   
  constructor(executor) {
   
    this.status = 'pending';
    this.value = undefined;
    this.reason = undefined;
    this.resolveCallback = [];
    this.rejectCallback = [];

    function resolve(value) {
   
      if (this.status === 'pending') {
   
        this.status = 'fulfilled';
        this.value = value;
        this.resolveCallback.forEach(fn => {
   
          fn();
        })
      }
    }

    function reject(reason) {
   
      if (this.status === 'pending') {
   
        this.status = 'rejected';
        this.reason = reason;
        this.rejectCallback.forEach(fn => {
   
          fn();
        })
      }
    }

    try {
   
      executor(resolve, reject);
    } catch (e) {
   
      reject(e);
    }
  }

  then(onFulfilled, onRejected){
   
    return new Promise((resolve, reject) => {
   
      if (this.status === 'fulfilled') {
   
        setTimeout(() => {
   
          const x = onFulfilled(
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值