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

前端面试中高频手撕[待补充]

以下是前端面试中高频手撕题目分类整理,结合字节跳动、得物等大厂真题及代码实现,建议重点掌握:


一、JS核心手写题

  1. 防抖与节流
    字节高频
// 防抖(最后一次触发后执行)
function debounce(fn, delay=500) {let timer = null;return (...args) => {if (timer) clearTimeout(timer);timer = setTimeout(() => fn.apply(this, args), delay);};
}// 节流(固定时间间隔执行)
function throttle(fn, delay=200) {let flag = true;return (...args) => {if (!flag) return;flag = false;setTimeout(() => {fn.apply(this, args);flag = true;}, delay);};
}
  1. Promise核心
    得物高频
// 手写Promise.allSettled(得物真题)
Promise.allSettled = promises => {return new Promise(resolve => {const res = [];let count = 0;promises.forEach((p, i) => {Promise.resolve(p).then(val => res[i] = { status: 'fulfilled', value: val },err => res[i] = { status: 'rejected', reason: err }).finally(() => (++count === promises.length) && resolve(res));});});
};

二、原型与继承

  1. 手写instanceof
function myInstanceof(obj, constructor) {let proto = Object.getPrototypeOf(obj);const prototype = constructor.prototype;while (proto) {if (proto === prototype) return true;proto = Object.getPrototypeOf(proto);}return false;
}
  1. 手写new操作符
function myNew(Fn, ...args) {const obj = Object.create(Fn.prototype);const result = Fn.apply(obj, args);return result instanceof Object ? result : obj;
}

三、函数与作用域

  1. call/apply/bind实现
// call(字节真题)
Function.prototype._call = function(context, ...args) {context = context || window;const fn = Symbol();context[fn] = this;const result = context[fn](...args);delete context[fn];return result;
};

四、数据结构与算法

  1. LRU缓存(字节高频)
class LRUCache {constructor(capacity) {this.cache = new Map();this.capacity = capacity;}get(key) {if (!this.cache.has(key)) return -1;const val = this.cache.get(key);this.cache.delete(key);this.cache.set(key, val);return val;}put(key, val) {this.cache.delete(key);this.cache.set(key, val);if (this.cache.size > this.capacity) this.cache.delete(this.cache.keys().next().value);}
}
  1. 深拷贝(字节/得物高频)
function deepClone(obj, hash = new WeakMap()) {if (typeof obj !== 'object' || obj === null) return obj;if (hash.has(obj)) return hash.get(obj);const clone = Array.isArray(obj) ? [] : {};hash.set(obj, clone);for (const key in obj) {if (obj.hasOwnProperty(key)) clone[key] = deepClone(obj[key], hash);}return clone;
}

五、框架相关(React/Vue)

  1. 有效括号问题(字节真题)
// 最长有效括号(动态规划)
function longestValidParentheses(s) {const dp = new Array(s.length).fill(0);let max = 0;for (let i = 1; i < s.length; i++) {if (s[i] === ')') {if (s[i-1] === '(') {dp[i] = (i >= 2 ? dp[i-2] : 0) + 2;} else if (i - dp[i-1] > 0 && s[i - dp[i-1] -1] === '(') {dp[i] = dp[i-1] + 2 + (i - dp[i-1] >=2 ? dp[i - dp[i-1] -2] : 0);}max = Math.max(max, dp[i]);}}return max;
}

六、高频附加题

  1. 数组扁平化

    function flatten(arr) {return arr.reduce((acc, cur) => acc.concat(Array.isArray(cur) ? flatten(cur) : cur), []);
    }
    
  2. 柯里化函数

    const curry = fn => judge = (...args) => args.length >= fn.length ?fn(...args) : (...arg) => judge(...args, ...arg);
    

大厂真题重点标注

公司高频考点真题来源
字节跳动LRU缓存、React Fiber、Promise链式调度
得物Promise.allSettled、Webpack优化

建议结合LeetCode高频题(如有效括号、三数之和)和框架原理(如React Hooks实现)进行扩展练习。更多完整代码和思路可参考:前端面试手撕题整理。

相关文章:

  • BR_频谱20dB 带宽(RF/TRM/CA/BV-05-C [TX Output Spectrum – 20 dB Bandwidth])
  • RAG工程-基于LangChain 实现 Naive RAG
  • 从GET到POST:HTTP请求的攻防实战与CTF挑战解析
  • 嵌入式linux系统中内存管理的方法与实现
  • 筑基挑战 | 第14期
  • UI文件上传
  • AI与IT的共生
  • 小测验——已经能利用数据集里面的相机外参调整后看到渲染图像
  • 网页聊天系统项目
  • 谷歌新域名结构:Hreflang的未来展望
  • C++ 基于多设计模式下的同步异步⽇志系统-1准备工作
  • 闩锁效应(latch up)
  • bat脚本转换为EXE应用程序文件
  • systemctl管理指令
  • opencv 给图片和视频添加水印
  • MySQL运维三部曲初级篇:从零开始打造稳定高效的数据库环境
  • Dify快速入门之chatflow
  • Linux网络编程——基于ET模式下的Reactor
  • 【正则表达式】正则表达式使用总结
  • 如何在3090显卡上使用老版本torch
  • 人民日报:对科研不端行为加大惩处力度,让造假成本远高于收益
  • 30小时已过,俄罗斯复活节停火不再延长
  • 南方将迎三轮降雨,两广旱区的“解渴雨”也要来了
  • 林诗栋4比1战胜梁靖崑,晋级世界杯男单决赛将和雨果争冠
  • 报告:去年物业服务百强企业营业收入均值同比增长3.52%
  • 租到“甲醛房”,租客可以解约吗?租金能要回来吗?