前端面试常见手写题整理

本文整理了前端面试中常见的手写题,包括bind、apply、call、instanceof、new、jsonp、Promise系列、EventEmitter、setTimeout实现setInterval、深拷贝、数组拍平、函数防抖、节流、柯里化以及排序算法等,旨在帮助面试者巩固基础,提升技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端面试手写题

整理前端面试常见的手写题,面试出现概率极高,建议每个都过自己过一遍,加深印象。

你可以 watch 一下这个 GitHub 仓库,会持续收集、整理和维护。

在这里插入图片描述

实现 bind()

Function.prototype.MyBind = function (context, ...args) {
   
  let self = this;
  return function() {
   
    return self.apply(context, args);
  }
}

// test
let a = {
   name: 'jack'} 
let test = function() {
   
  console.log(this.name); // jack
}
let rs = test.MyBind(a);
rs();

实现 apply()

Function.prototype.myApply = function (context, args) {
   
  context.fn = this;
  let res;
  if (!args){
   
    res = context.fn();
  } else  {
   
    res = context.fn(...args)
  }
  return res;
}

// test
let obj = {
   
  name: 'jack'
}
function test(arg1, arg2, arg3) {
   
  console.log(this.name)   // jack
  console.log(arg1, arg2, arg3);  // 1 2 3
}
test.myApply(obj, [1,2,3]);

实现 call()

Function.prototype.myCall = function (context, ...rest) {
   
  context.fn = this;
  var result = context.fn(...rest);
  delete context.fn;
  return result;
}

// test
let obj = {
   
  name: 'jack'
}
function test(arg1, arg2, arg3) {
   
  console.log(this.name)   // jack
  console.log(arg1, arg2, arg3);  // 1 2 3
}
test.myCall(obj, 1,2,3);

实现 instanceof

function myInstanceOf(left, right) {
   
  let prototype = right.prototype;
  left = left.__proto__;
  while(true) {
   
    if (!left) return false;
    if (left == prototype) return true;
    left = left.__proto__;
  }
}

console.log(myInstanceOf([], Array));  // true

实现 new

function myNew (fun, ...args) {
   
  let obj =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值