当前位置: 首页 > news >正文

JS 手撕题高频考点

前端面试中,JS 手撕题是高频考点,主要考察 编程能力、算法思维、JS 核心知识。以下是最常见的手撕题分类 + 代码示例


目录

    • 📌 1. 手写函数柯里化
    • 📌 2. 手写 `debounce`(防抖)
    • 📌 3. 手写 `throttle`(节流)
    • 📌 4. 手写深拷贝
    • 📌 5. 手写 `new` 操作符
    • 📌 6. 手写 `call` / `apply` / `bind`
    • 📌 7. 手写 `Promise.all`
    • 📌 8. 实现 LRU 缓存
    • 📌 9. 手写 `instanceof`
    • 📌 10. 数组去重

📌 1. 手写函数柯里化

题目:实现一个 curry(fn) 函数

function curry(fn, ...args) {
  return args.length >= fn.length
    ? fn(...args)
    : (...nextArgs) => curry(fn, ...args, ...nextArgs);
}

// 示例函数
function sum(a, b, c) {
  return a + b + c;
}

const curriedSum = curry(sum);
console.log(curriedSum(1)(2)(3)); // 6
console.log(curriedSum(1, 2)(3)); // 6

📌 2. 手写 debounce(防抖)

题目:实现 debounce(fn, delay)

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

// 示例:输入框搜索
const search = debounce(() => console.log("搜索 API 请求"), 500);
document.getElementById("searchInput").addEventListener("input", search);

📌 3. 手写 throttle(节流)

题目:实现 throttle(fn, delay)

function throttle(fn, delay) {
  let lastTime = 0;
  return function (...args) {
    const now = Date.now();
    if (now - lastTime >= delay) {
      fn.apply(this, args);
      lastTime = now;
    }
  };
}

// 示例:鼠标滚动触发
const handleScroll = throttle(() => console.log("滚动中..."), 1000);
window.addEventListener("scroll", handleScroll);

📌 4. 手写深拷贝

题目:实现 deepClone(obj)

function deepClone(obj, map = new WeakMap()) {
  if (typeof obj !== "object" || obj === null) return obj;
  if (map.has(obj)) return map.get(obj); // 处理循环引用
  let clone = Array.isArray(obj) ? [] : {};
  map.set(obj, clone);
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      clone[key] = deepClone(obj[key], map);
    }
  }
  return clone;
}

// 示例
const obj = { a: 1, b: { c: 2 } };
const copy = deepClone(obj);
console.log(copy);

📌 5. 手写 new 操作符

题目:实现 myNew(fn, ...args)

function myNew(fn, ...args) {
  const obj = Object.create(fn.prototype); // 创建空对象并继承构造函数原型
  const result = fn.apply(obj, args); // 执行构造函数
  return result instanceof Object ? result : obj; // 处理返回值
}

// 示例
function Person(name) {
  this.name = name;
}
const p = myNew(Person, "Alice");
console.log(p.name); // Alice

📌 6. 手写 call / apply / bind

✅ 手写 call

Function.prototype.myCall = function (context, ...args) {
  context = context || window;
  const fnKey = Symbol();
  context[fnKey] = this;
  const result = context[fnKey](...args);
  delete context[fnKey];
  return result;
};

// 示例
function greet() {
  console.log(`Hello, ${this.name}`);
}
const obj = { name: "Alice" };
greet.myCall(obj); // Hello, Alice

✅ 手写 apply

Function.prototype.myApply = function (context, args = []) {
  context = context || window;
  const fnKey = Symbol();
  context[fnKey] = this;
  const result = context[fnKey](...args);
  delete context[fnKey];
  return result;
};

✅ 手写 bind

Function.prototype.myBind = function (context, ...args) {
  const self = this;
  return function (...innerArgs) {
    return self.apply(context, [...args, ...innerArgs]);
  };
};

// 示例
const boundFn = greet.myBind(obj);
boundFn(); // Hello, Alice

📌 7. 手写 Promise.all

题目:实现 myPromiseAll(promises)

function myPromiseAll(promises) {
  return new Promise((resolve, reject) => {
    let results = [];
    let count = 0;
    if (promises.length === 0) resolve([]);
    promises.forEach((p, index) => {
      Promise.resolve(p).then(
        value => {
          results[index] = value;
          count++;
          if (count === promises.length) resolve(results);
        },
        error => reject(error)
      );
    });
  });
}

// 示例
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = new Promise(resolve => setTimeout(() => resolve(3), 1000));

myPromiseAll([p1, p2, p3]).then(console.log); // [1, 2, 3]

📌 8. 实现 LRU 缓存

题目:设计一个 LRUCache

class LRUCache {
  constructor(capacity) {
    this.capacity = capacity;
    this.cache = new Map();
  }
  
  get(key) {
    if (!this.cache.has(key)) return -1;
    let value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value);
    return value;
  }
  
  put(key, value) {
    if (this.cache.has(key)) this.cache.delete(key);
    this.cache.set(key, value);
    if (this.cache.size > this.capacity) {
      this.cache.delete(this.cache.keys().next().value); // 删除最早的键
    }
  }
}

// 示例
const cache = new LRUCache(2);
cache.put(1, "A");
cache.put(2, "B");
console.log(cache.get(1)); // A
cache.put(3, "C"); // 淘汰 key=2
console.log(cache.get(2)); // -1

📌 9. 手写 instanceof

题目:实现 myInstanceOf(obj, constructor)

function myInstanceOf(obj, constructor) {
  let proto = Object.getPrototypeOf(obj);
  while (proto) {
    if (proto === constructor.prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
  return false;
}

// 示例
console.log(myInstanceOf([], Array)); // true
console.log(myInstanceOf({}, Array)); // false

📌 10. 数组去重

const uniqueArray = arr => [...new Set(arr)];
console.log(uniqueArray([1, 2, 2, 3])); // [1, 2, 3]

相关文章:

  • MATLAB详细图文安装教程(附安装包)
  • 域渗透(web安全)
  • 归并排序与快速排序的简单实现(C语言)
  • 前端自动创建react项目脚手架
  • 中国太平保险网申校招测评笔试真题分析、历年真题题库、北森答案解析
  • golang 日志log与logrus
  • Android开发中的数据结构与算法:树与图
  • 音视频 YUV格式详解
  • Web开发-JS应用微信小程序源码架构编译预览逆向调试嵌套资产代码审计
  • 孤码长征:破译PCL自定义点云注册机制源码迷局——踩坑实录与架构解构
  • grafana 配置页面告警
  • 【LLM】Elasticsearch作为向量库入门指南
  • IDEA 终端 vs CMD:为什么 java -version 显示的 JDK 版本不一致?
  • 电子文档安全管理系统V6.0接口backup存在任意文件下载漏洞
  • golang 的strconv包常用方法
  • J2EE框架技术第八章 SpringMVC框架技术
  • HarmonyOS 介绍
  • Ubuntu在VMware中无法全屏
  • MIPS-32架构(寄存器堆,指令系统,运算器)
  • 网络空间安全(45)PHP入门学习
  • 在线设计签名免费艺术签名/西安百度推广优化托管
  • 舆情服务网站/西安百度竞价托管
  • 做网站要用服务器吗/国内新闻最新消息今天
  • 东莞微网站制作公司/广州seo优化公司
  • 嵩县网站开发/荆门网络推广
  • 买好域名之后怎么做网站/珠海优化seo