code
1、根据以下代码进行对应操作
tree是一个层级不固定的树形结构,每个节点包含key、count和children属性。编写一段代码,递归计算每个节点的count值,count值表示以该节点为根的子树中包含的节点总数(包含当前节点自身)。
点击查看实例代码
const tree = {
key: "0",
count: 0,
children: [
{
key: "0-0",
count: 0,
children: [
{
key: "0-0-0",
count: 0,
children: [],
},
{
key: "0-0-1",
count: 0,
children: [],
},
],
},
{
key: "0-1",
count: 0,
children: [],
},
],
};详情
/**
* 递归计算树形结构中每个节点的 count 值
* count 值表示以该节点为根的子树中包含的节点总数(包含当前节点自身)
* @param {Object} node - 当前需要计算 count 值的节点,该节点包含 key、count 和 children 属性
* @returns {Object} - 返回计算好 count 值后的当前节点
*/
const calculateCount = node => {
// 获取当前节点的子节点数组长度
const length = node.children.length;
// 如果子节点数组长度为 0,说明当前节点没有子节点
if (!length) {
// 没有子节点时,count 值仅包含当前节点自身,所以设为 1
node.count = 1;
// 返回计算好 count 值的当前节点
return node;
}
// 初始化一个变量 total,用于累加所有子节点的 count 值
let total = 0;
// 遍历当前节点的所有子节点
for (let index = 0; index < length; index++) {
// 获取当前遍历到的子节点
const child = node.children[index];
// 递归调用 calculateCount 函数,计算子节点的 count 值
calculateCount(child);
// 将子节点计算好的 count 值累加到 total 中
total += child.count;
}
// 当前节点的 count 值等于所有子节点的 count 值总和加上自身(即加 1)
node.count = total + 1;
// 返回计算好 count 值的当前节点
return node;
};
const result = calculateCount(tree);
console.log("🚀 ~ index.js:39 ~ result:", result);2、手写防抖函数
- 防抖函数:防抖函数是指在事件被触发n秒后再执行回调,如果在这n秒内事件又被触发,则重新开始延时。
- 应用场景:
- 搜索框输入。
- 窗口resize。
- 按钮点击。
- 滚动事件。
- 实现原理:
- 利用闭包保存定时器。
- 每次触发事件时,先清除之前的定时器,再重新设置定时器。
- 定时器到期后,执行回调函数。
详情
function debounce(fn, delay) {
let timer = null;
return function (...args) {
// 每次触发事件时,先清除之前的定时器
if (timer) {
clearTimeout(timer);
}
// 重新设置定时器
timer = setTimeout(() => {
// 定时器到期后,执行回调函数,绑定当前上下文和参数
fn.apply(this, args);
}, delay);
};
}
// 测试防抖函数
const testFn = debounce(() => {
console.log("防抖函数触发");
}, 1000);
// 模拟连续触发事件
testFn(); // 1秒后触发
testFn(); // 1秒后触发
testFn(); // 1秒后触发3、手写节流函数
- 节流函数:节流函数是指在事件被触发n秒内只执行一次回调。
- 应用场景:
- 按钮点击。
- 窗口resize。
- 滚动事件。
- 实现原理:
- 利用闭包保存上次执行时间。
- 每次触发事件时,获取当前时间与上次执行时间的差值。
- 如果差值大于等于设定的时间间隔,则执行回调函数,并更新上次执行时间。
详情
function throttle(fn, delay) {
let lastTime = 0; // 上一次执行函数的时间戳,初始为 0
return function (...args) {
// 当前时间戳
const nowTime = Date.now();
// 计算当前时间与上次执行时间的差值,
// 如果差值大于等于设定的时间间隔,则执行回调函数,并更新上次执行时间
if (nowTime - lastTime >= delay) {
// 执行回调函数,绑定当前上下文和参数
fn.apply(this, args);
// 更新上次执行时间为当前时间
lastTime = nowTime;
}
// 如果差值小于设定的时间间隔,则不执行回调函数
};
}4、手写柯里化函数
- 柯里化函数:柯里化函数是指将一个多参数函数转换为多个单参数函数的技术。
- 应用场景:
- 函数参数复用。
- 函数参数延迟执行。
- 实现原理:
- 利用闭包保存参数。
- 每次调用函数时,判断参数是否足够。
- 如果参数足够,则执行回调函数。
- 如果参数不足,则返回新的函数,等待参数补充。
详情
function currying(fn, ...args) {
// 如果参数足够,则执行回调函数
if (args.length >= fn.length) {
return fn.apply(this, args);
}
// 如果参数不足,则返回新的函数,等待参数补充
// return currying.bind(this, fn, ...args);
return (...newArgs) => currying(fn, ...args, ...newArgs);
}
// 测试柯里化函数
const testFn = currying((a, b, c) => {
console.log("柯里化函数触发", a, b, c);
});
// 模拟连续触发事件
testFn(1)(2)(3); // 柯里化函数触发 1 2 3
testFn(1, 2)(3); // 柯里化函数触发 1 2 3
testFn(1, 2, 3); // 柯里化函数触发 1 2 35、手写冒泡排序
- 冒泡排序:冒泡排序是一种简单的排序算法,它重复地遍历待排序的数列,比较相邻元素并交换顺序错误的元素,直到没有需要交换的元素为止。
- 应用场景:
- 数组排序。
- 实现原理:
- 从第一个元素开始,比较相邻的两个元素。
- 如果第一个元素大于第二个元素,则交换它们的位置。
- 对每一对相邻的元素做同样的工作,从开始第一对到结尾的最后一对,最后的元素会是最大的数。
- 针对所有的元素重复以上的步骤,除了最后一个。
- 重复以上步骤,直到没有需要交换的元素为止。
- 优化:
- 可以在每一轮排序中,记录最后一次交换的位置,作为下一轮排序的边界。
- 可以添加一个标志位,用于判断是否发生了交换。如果没有发生交换,则说明数组已经有序,直接结束排序。
- 时间复杂度:
- 平均时间复杂度:O(n^2)
- 最坏时间复杂度:O(n^2)
- 最好时间复杂度:O(n)
- 空间复杂度:
- O(1)
- 稳定性:
- 稳定排序算法。
详情
// 基础版,时间复杂度O(n^2)
function bubbleSort(arr) {
const len = arr.length;
// 外层循环,控制排序轮数
for (let i = 0; i < len - 1; i++) {
// 内层循环,控制每轮排序的比较次数
for (let j = 0; j < len - 1 - i; j++) {
// 比较相邻元素,如果前一个比后一个大
if (arr[j] > arr[j + 1]) {
// 交换元素位置(升序)
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
// 返回排序后的数组
return arr;
}
const arr = [5, 3, 8, 4, 2];
const sortedArr = bubbleSort(arr);
console.log("🚀 ~ index.js:39 ~ sortedArr:", sortedArr);
// 进阶版,时间复杂度可以优化到 O(n)
// 优化思路:如果某一轮没有任何交换,说明数组已经有序,可以提前退出!
function bubbleSortOptimized(arr) {
const len = arr.length;
let swapped;
for (let i = 0; i < len - 1; i++) {
swapped = false;
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
swapped = true;
}
}
if (!swapped) break; // 本轮无交换,说明已有序,直接退出
}
return arr;
}6、手写深拷贝函数
- 深拷贝:深拷贝是指创建一个新的对象或数组,并递归复制原数据中的每一层属性,使新数据和原数据不共享引用地址。
- 应用场景:
- 复制复杂对象,避免修改副本时影响原对象。
- 状态管理中保存快照。
- 表单数据回显、重置和编辑。
- 实现原理:
- 判断当前值是否为对象类型,如果不是对象则直接返回。
- 判断当前值是数组还是普通对象,创建对应的新容器。
- 遍历对象自身属性,递归拷贝每一项。
- 使用 WeakMap 记录已经拷贝过的对象,解决循环引用问题。
- 注意事项:
- 基础版本主要处理普通对象和数组。
- Date、RegExp、Map、Set、函数、Symbol 等特殊类型需要按业务场景单独处理。
详情
function deepClone(target, cache = new WeakMap()) {
// null 和基本数据类型直接返回
if (target === null || typeof target !== "object") {
return target;
}
// 处理循环引用,如果当前对象已经拷贝过,直接返回缓存结果
if (cache.has(target)) {
return cache.get(target);
}
// 根据原数据类型创建新的数组或对象
const result = Array.isArray(target) ? [] : {};
// 先放入缓存,避免递归过程中遇到循环引用时无限递归
cache.set(target, result);
// 只遍历对象自身属性
for (const key in target) {
if (Object.prototype.hasOwnProperty.call(target, key)) {
result[key] = deepClone(target[key], cache);
}
}
return result;
}
// 测试深拷贝函数
const obj = {
name: "前端",
info: {
age: 3,
tags: ["html", "css", "js"],
},
};
obj.self = obj;
const clonedObj = deepClone(obj);
clonedObj.info.tags.push("vue");
console.log(obj.info.tags); // ["html", "css", "js"]
console.log(clonedObj.info.tags); // ["html", "css", "js", "vue"]
console.log(clonedObj.self === clonedObj); // true7、手写数组扁平化函数
- 数组扁平化:数组扁平化是指将多层嵌套数组转换为指定层级或一维数组。
- 应用场景:
- 处理树形数据转换后的列表结果。
- 合并多层分类、菜单或权限数组。
- 面试中考察递归、循环和数组方法的使用。
- 实现原理:
- 遍历数组中的每一项。
- 如果当前项还是数组,并且还需要继续展开,则递归处理当前项。
- 如果当前项不是数组,或者已经达到指定展开层级,则直接放入结果数组。
- 注意事项:
- 可以通过 depth 控制展开层级。
- 如果需要完全展开,可以传入 Infinity。
详情
function flatten(arr, depth = 1) {
const result = [];
for (const item of arr) {
if (Array.isArray(item) && depth > 0) {
result.push(...flatten(item, depth - 1));
} else {
result.push(item);
}
}
return result;
}
// 测试数组扁平化函数
const arr = [1, [2, [3, [4, 5]]], 6];
console.log(flatten(arr)); // [1, 2, [3, [4, 5]], 6]
console.log(flatten(arr, 2)); // [1, 2, 3, [4, 5], 6]
console.log(flatten(arr, Infinity)); // [1, 2, 3, 4, 5, 6]8、手写发布订阅模式
- 发布订阅模式:发布订阅模式是一种对象间通信方式,发布者不直接调用订阅者,而是通过事件中心统一管理事件的订阅、触发和取消订阅。
- 应用场景:
- 组件之间解耦通信。
- 全局事件总线。
- 消息通知、状态变化通知。
- 实现原理:
- 使用一个对象或 Map 保存事件名和对应的回调函数列表。
- 订阅时,将回调函数加入对应事件的列表。
- 发布时,依次执行该事件下的所有回调函数。
- 取消订阅时,从回调函数列表中移除指定回调。
- 注意事项:
- 触发事件时需要透传参数。
- 取消订阅时要处理事件不存在或回调不存在的情况。
- 可以扩展 once 方法,实现只执行一次的订阅。
详情
class EventEmitter {
constructor() {
this.events = new Map();
}
on(eventName, callback) {
if (!this.events.has(eventName)) {
this.events.set(eventName, []);
}
this.events.get(eventName).push(callback);
}
off(eventName, callback) {
const callbacks = this.events.get(eventName);
if (!callbacks) return;
const index = callbacks.indexOf(callback);
if (index !== -1) {
callbacks.splice(index, 1);
}
if (callbacks.length === 0) {
this.events.delete(eventName);
}
}
emit(eventName, ...args) {
const callbacks = this.events.get(eventName);
if (!callbacks) return;
callbacks.slice().forEach(callback => {
callback(...args);
});
}
once(eventName, callback) {
const onceCallback = (...args) => {
callback(...args);
this.off(eventName, onceCallback);
};
this.on(eventName, onceCallback);
}
}
// 测试发布订阅模式
const eventBus = new EventEmitter();
function handleMessage(message) {
console.log("收到消息:", message);
}
eventBus.on("message", handleMessage);
eventBus.emit("message", "hello"); // 收到消息:hello
eventBus.off("message", handleMessage);
eventBus.emit("message", "world"); // 不会执行
eventBus.once("login", username => {
console.log("登录用户:", username);
});
eventBus.emit("login", "Tom"); // 登录用户:Tom
eventBus.emit("login", "Jerry"); // 不会执行9、手写 instanceof
- instanceof:instanceof 用于判断构造函数的 prototype 属性是否出现在实例对象的原型链上。
- 应用场景:
- 判断对象是否由某个构造函数创建。
- 判断对象是否属于某个类或父类。
- 面试中考察原型链和构造函数的理解。
- 实现原理:
- 先获取实例对象的原型,也就是
Object.getPrototypeOf(left)。 - 再获取构造函数的
prototype。 - 沿着实例对象的原型链不断向上查找。
- 如果某一层原型等于构造函数的
prototype,则返回 true。 - 如果原型链查找到 null,说明没有找到,返回 false。
- 先获取实例对象的原型,也就是
- 注意事项:
- 基本数据类型不是对象,直接返回 false。
- 右侧必须是函数,否则不符合 instanceof 的使用方式。
详情
function myInstanceof(left, right) {
if (left === null || (typeof left !== "object" && typeof left !== "function")) {
return false;
}
if (typeof right !== "function") {
throw new TypeError("Right-hand side of instanceof is not callable");
}
let proto = Object.getPrototypeOf(left);
const prototype = right.prototype;
while (proto !== null) {
if (proto === prototype) {
return true;
}
proto = Object.getPrototypeOf(proto);
}
return false;
}
// 测试 instanceof
function Person(name) {
this.name = name;
}
const person = new Person("Tom");
console.log(myInstanceof(person, Person)); // true
console.log(myInstanceof(person, Object)); // true
console.log(myInstanceof([], Array)); // true
console.log(myInstanceof([], Object)); // true
console.log(myInstanceof("hello", String)); // false10、手写 new 操作符
- new 操作符:new 用于创建构造函数的实例对象,并将实例对象的原型指向构造函数的 prototype。
- 应用场景:
- 理解构造函数创建实例的过程。
- 理解 this、原型链和返回值之间的关系。
- 面试中常用于考察 JavaScript 对象创建机制。
- 实现原理:
- 创建一个新的空对象。
- 将新对象的原型指向构造函数的 prototype。
- 使用新对象作为 this 执行构造函数,并传入参数。
- 如果构造函数返回对象或函数,则返回该结果。
- 如果构造函数没有返回对象,则返回创建的新对象。
- 注意事项:
- 构造函数返回基本数据类型时会被忽略。
- 构造函数返回对象或函数时,会覆盖默认创建的实例对象。
详情
function myNew(Constructor, ...args) {
if (typeof Constructor !== "function") {
throw new TypeError("Constructor must be a function");
}
// 创建一个新对象,并让它的原型指向构造函数的 prototype
const instance = Object.create(Constructor.prototype);
// 执行构造函数,让构造函数中的 this 指向新对象
const result = Constructor.apply(instance, args);
// 如果构造函数返回对象或函数,则返回该结果,否则返回新对象
if (result !== null && (typeof result === "object" || typeof result === "function")) {
return result;
}
return instance;
}
// 测试 new 操作符
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayName = function () {
console.log(this.name);
};
const person = myNew(Person, "Tom", 18);
console.log(person.name); // Tom
console.log(person.age); // 18
console.log(person instanceof Person); // true
person.sayName(); // Tom
function Factory() {
this.name = "default";
return {
name: "custom",
};
}
console.log(myNew(Factory).name); // custom11、手写 call 方法
- call 方法:call 用于改变函数执行时的 this 指向,并立即执行函数,参数以逗号分隔的形式传入。
- 应用场景:
- 显式指定函数执行时的 this。
- 借用其他对象上的方法。
- 面试中考察 this 绑定、对象属性调用和参数处理。
- 实现原理:
- 判断调用 myCall 的目标是否为函数。
- 将传入的 thisArg 转成对象,null 或 undefined 默认指向全局对象。
- 使用 Symbol 创建唯一属性名,避免覆盖 thisArg 上已有属性。
- 将当前函数临时挂到 thisArg 上并执行。
- 执行完成后删除临时属性,并返回函数执行结果。
- 注意事项:
- 基本数据类型作为 thisArg 时,需要通过 Object 包装成对象。
- 使用 Symbol 可以避免属性名冲突。
详情
Function.prototype.myCall = function (thisArg, ...args) {
if (typeof this !== "function") {
throw new TypeError("myCall must be called on a function");
}
const context = thisArg == null ? globalThis : Object(thisArg);
const fnKey = Symbol("fn");
context[fnKey] = this;
const result = context[fnKey](...args);
delete context[fnKey];
return result;
};
// 测试 call 方法
function introduce(city, job) {
return `${this.name} 来自 ${city},职业是 ${job}`;
}
const user = {
name: "Tom",
};
const result = introduce.myCall(user, "上海", "前端工程师");
console.log(result); // Tom 来自 上海,职业是 前端工程师
function getType() {
return Object.prototype.toString.myCall(this);
}
console.log(getType.myCall([])); // [object Array]12、手写 apply 方法
- apply 方法:apply 用于改变函数执行时的 this 指向,并立即执行函数,参数以数组或类数组的形式传入。
- 应用场景:
- 显式指定函数执行时的 this。
- 参数已经以数组形式存在时调用函数。
- 借用内置方法处理类数组。
- 实现原理:
- 判断调用 myApply 的目标是否为函数。
- 将传入的 thisArg 转成对象,null 或 undefined 默认指向全局对象。
- 使用 Symbol 创建唯一属性名,将当前函数临时挂到 thisArg 上。
- 判断第二个参数是否存在,不存在则直接执行函数。
- 如果第二个参数存在,则展开参数数组后执行函数。
- 执行完成后删除临时属性,并返回函数执行结果。
- 注意事项:
- 第二个参数必须是数组或类数组。
- apply 和 call 的核心区别在于参数传递方式不同。
详情
Function.prototype.myApply = function (thisArg, args) {
if (typeof this !== "function") {
throw new TypeError("myApply must be called on a function");
}
const context = thisArg == null ? globalThis : Object(thisArg);
const fnKey = Symbol("fn");
context[fnKey] = this;
let result;
if (args == null) {
result = context[fnKey]();
} else {
result = context[fnKey](...args);
}
delete context[fnKey];
return result;
};
// 测试 apply 方法
function introduce(city, job) {
return `${this.name} 来自 ${city},职业是 ${job}`;
}
const user = {
name: "Tom",
};
const result = introduce.myApply(user, ["上海", "前端工程师"]);
console.log(result); // Tom 来自 上海,职业是 前端工程师
const numbers = [5, 10, 2, 8];
console.log(Math.max.myApply(null, numbers)); // 1013、手写 bind 方法
- bind 方法:bind 用于改变函数执行时的 this 指向,但不会立即执行函数,而是返回一个新的函数。
- 应用场景:
- 固定函数执行时的 this。
- 预置部分参数,实现函数参数复用。
- 在事件回调、定时器回调中保留 this 指向。
- 实现原理:
- 判断调用 myBind 的目标是否为函数。
- 保存原函数、绑定的 thisArg 和预置参数。
- 返回一个新函数,新函数执行时合并预置参数和调用参数。
- 如果返回的新函数被 new 调用,this 应该指向新创建的实例,而不是绑定的 thisArg。
- 维护原函数的原型关系,使 new 调用时实例可以访问原型方法。
- 注意事项:
- bind 返回的是函数,不会立即执行。
- bind 支持参数分批传入。
- bind 后的函数作为构造函数使用时,绑定的 this 会失效。
详情
Function.prototype.myBind = function (thisArg, ...bindArgs) {
if (typeof this !== "function") {
throw new TypeError("myBind must be called on a function");
}
const originalFn = this;
function boundFn(...callArgs) {
const isNewCall = this instanceof boundFn;
const context = isNewCall ? this : thisArg;
return originalFn.apply(context, [...bindArgs, ...callArgs]);
}
// 维护原型关系,保证 new boundFn() 创建的实例可以访问原函数原型上的方法
boundFn.prototype = Object.create(originalFn.prototype);
boundFn.prototype.constructor = boundFn;
return boundFn;
};
// 测试 bind 方法
function introduce(city, job) {
return `${this.name} 来自 ${city},职业是 ${job}`;
}
const user = {
name: "Tom",
};
const boundIntroduce = introduce.myBind(user, "上海");
console.log(boundIntroduce("前端工程师")); // Tom 来自 上海,职业是 前端工程师
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayName = function () {
console.log(this.name);
};
const BoundPerson = Person.myBind({ name: "绑定对象" }, "Jerry");
const person = new BoundPerson(20);
console.log(person.name); // Jerry
console.log(person.age); // 20
console.log(person instanceof Person); // true
person.sayName(); // Jerry14、手写 Promise.all
- Promise.all:Promise.all 用于并发执行多个 Promise,全部成功后按原顺序返回结果数组,只要有一个失败就立即返回失败原因。
- 应用场景:
- 多个接口并发请求,并等待全部完成。
- 多个异步任务互不依赖,但后续逻辑依赖全部结果。
- 面试中考察 Promise、并发控制和结果顺序处理。
- 实现原理:
- 返回一个新的 Promise。
- 遍历传入的可迭代对象,将每一项通过 Promise.resolve 包装。
- 每个 Promise 成功后,将结果保存到对应下标位置。
- 使用计数器记录已完成数量,全部完成后 resolve 结果数组。
- 任意一个 Promise 失败时,直接 reject 失败原因。
- 注意事项:
- 返回结果的顺序和传入顺序一致,不受完成时间影响。
- 空数组需要直接 resolve 空数组。
- 非 Promise 值也要当作成功值处理。
详情
function promiseAll(promises) {
return new Promise((resolve, reject) => {
const list = Array.from(promises);
const result = [];
let fulfilledCount = 0;
if (list.length === 0) {
resolve([]);
return;
}
list.forEach((item, index) => {
Promise.resolve(item)
.then(value => {
result[index] = value;
fulfilledCount++;
if (fulfilledCount === list.length) {
resolve(result);
}
})
.catch(reason => {
reject(reason);
});
});
});
}
// 测试 Promise.all
const p1 = Promise.resolve(1);
const p2 = new Promise(resolve => {
setTimeout(() => resolve(2), 1000);
});
const p3 = 3;
promiseAll([p1, p2, p3]).then(result => {
console.log(result); // [1, 2, 3]
});
const p4 = Promise.reject("error");
promiseAll([p1, p4, p3]).catch(error => {
console.log(error); // error
});15、手写 Promise.race
- Promise.race:Promise.race 用于并发执行多个 Promise,谁最先完成就以谁的结果作为最终结果,最先完成可以是成功也可以是失败。
- 应用场景:
- 请求超时控制。
- 多个异步任务竞争,只关心最快返回的结果。
- 面试中考察 Promise 状态流转和并发处理。
- 实现原理:
- 返回一个新的 Promise。
- 遍历传入的可迭代对象,将每一项通过 Promise.resolve 包装。
- 任意一个 Promise 最先 resolve 时,外层 Promise 直接 resolve。
- 任意一个 Promise 最先 reject 时,外层 Promise 直接 reject。
- Promise 状态一旦确定,后续结果不会再影响最终状态。
- 注意事项:
- 非 Promise 值会被 Promise.resolve 包装,可能最先完成。
- 空数组不会触发 resolve 或 reject,会一直处于 pending 状态。
详情
function promiseRace(promises) {
return new Promise((resolve, reject) => {
const list = Array.from(promises);
list.forEach(item => {
Promise.resolve(item).then(resolve, reject);
});
});
}
// 测试 Promise.race
const p1 = new Promise(resolve => {
setTimeout(() => resolve("p1"), 1000);
});
const p2 = new Promise(resolve => {
setTimeout(() => resolve("p2"), 500);
});
promiseRace([p1, p2]).then(result => {
console.log(result); // p2
});
const request = new Promise(resolve => {
setTimeout(() => resolve("请求成功"), 2000);
});
const timeout = new Promise((resolve, reject) => {
setTimeout(() => reject("请求超时"), 1000);
});
promiseRace([request, timeout]).catch(error => {
console.log(error); // 请求超时
});16、手写 Promise.allSettled
- Promise.allSettled:Promise.allSettled 用于并发执行多个 Promise,等所有 Promise 都完成后,返回每一项的最终状态和结果。
- 应用场景:
- 多个请求互不影响,需要拿到每个请求的成功或失败结果。
- 批量任务执行后展示完整执行报告。
- 面试中考察 Promise 状态收集和结果顺序处理。
- 实现原理:
- 返回一个新的 Promise。
- 遍历传入的可迭代对象,将每一项通过 Promise.resolve 包装。
- 成功时记录
{ status: "fulfilled", value }。 - 失败时记录
{ status: "rejected", reason }。 - 使用计数器记录已完成数量,全部完成后 resolve 结果数组。
- 注意事项:
- 不会因为某一个 Promise 失败而 reject。
- 返回结果的顺序和传入顺序一致。
- 空数组需要直接 resolve 空数组。
详情
function promiseAllSettled(promises) {
return new Promise(resolve => {
const list = Array.from(promises);
const result = [];
let settledCount = 0;
if (list.length === 0) {
resolve([]);
return;
}
list.forEach((item, index) => {
Promise.resolve(item)
.then(value => {
result[index] = {
status: "fulfilled",
value,
};
})
.catch(reason => {
result[index] = {
status: "rejected",
reason,
};
})
.finally(() => {
settledCount++;
if (settledCount === list.length) {
resolve(result);
}
});
});
});
}
// 测试 Promise.allSettled
const p1 = Promise.resolve("success");
const p2 = Promise.reject("error");
const p3 = 3;
promiseAllSettled([p1, p2, p3]).then(result => {
console.log(result);
// [
// { status: "fulfilled", value: "success" },
// { status: "rejected", reason: "error" },
// { status: "fulfilled", value: 3 }
// ]
});17、手写 Promise.any
- Promise.any:Promise.any 用于并发执行多个 Promise,只要有一个 Promise 成功,就以第一个成功的结果作为最终结果;只有全部失败时才会 reject。
- 应用场景:
- 多个备用接口同时请求,只使用最快成功的结果。
- 多个资源源站兜底,只要有一个可用即可。
- 面试中考察 Promise 状态处理和失败原因收集。
- 实现原理:
- 返回一个新的 Promise。
- 遍历传入的可迭代对象,将每一项通过 Promise.resolve 包装。
- 任意一个 Promise 成功时,外层 Promise 立即 resolve。
- 每个 Promise 失败时,将失败原因记录到对应下标位置。
- 如果全部 Promise 都失败,则 reject 一个 AggregateError。
- 注意事项:
- 和 Promise.race 不同,Promise.any 只关心第一个成功结果。
- 全部失败时才会 reject。
- 空数组会直接 reject AggregateError。
详情
function promiseAny(promises) {
return new Promise((resolve, reject) => {
const list = Array.from(promises);
const errors = [];
let rejectedCount = 0;
if (list.length === 0) {
reject(new AggregateError([], "All promises were rejected"));
return;
}
list.forEach((item, index) => {
Promise.resolve(item)
.then(value => {
resolve(value);
})
.catch(reason => {
errors[index] = reason;
rejectedCount++;
if (rejectedCount === list.length) {
reject(new AggregateError(errors, "All promises were rejected"));
}
});
});
});
}
// 测试 Promise.any
const p1 = Promise.reject("error1");
const p2 = new Promise(resolve => {
setTimeout(() => resolve("success"), 1000);
});
const p3 = Promise.reject("error3");
promiseAny([p1, p2, p3]).then(result => {
console.log(result); // success
});
promiseAny([Promise.reject("a"), Promise.reject("b")]).catch(error => {
console.log(error instanceof AggregateError); // true
console.log(error.errors); // ["a", "b"]
});18、手写 Promise.resolve
- Promise.resolve:Promise.resolve 用于将一个值转换为 fulfilled 状态的 Promise。如果传入的是 Promise,则直接返回它本身。
- 应用场景:
- 统一同步值和异步值的处理方式。
- 在 Promise.all、Promise.race 等方法中包装每一项输入。
- 将 thenable 对象转换为标准 Promise。
- 实现原理:
- 如果传入值本身就是当前 Promise 构造函数创建的 Promise,直接返回该值。
- 如果传入值是 thenable 对象或函数,则读取它的 then 方法。
- 如果 then 是函数,则调用 then,并根据 then 的执行结果决定 Promise 状态。
- 如果传入的是普通值,则返回 fulfilled 状态的 Promise。
- 注意事项:
- thenable 是指带有 then 方法的对象或函数。
- 读取 then 属性时可能抛错,需要进入 rejected 状态。
- thenable 的状态只能被改变一次。
详情
function promiseResolve(value) {
if (value instanceof Promise) {
return value;
}
return new Promise((resolve, reject) => {
if (value !== null && (typeof value === "object" || typeof value === "function")) {
let then;
try {
then = value.then;
} catch (error) {
reject(error);
return;
}
if (typeof then === "function") {
let called = false;
try {
then.call(
value,
result => {
if (called) return;
called = true;
resolve(result);
},
reason => {
if (called) return;
called = true;
reject(reason);
}
);
} catch (error) {
if (!called) {
reject(error);
}
}
return;
}
}
resolve(value);
});
}
// 测试 Promise.resolve
promiseResolve(1).then(result => {
console.log(result); // 1
});
const promise = Promise.resolve("success");
console.log(promiseResolve(promise) === promise); // true
const thenable = {
then(resolve) {
resolve("thenable success");
},
};
promiseResolve(thenable).then(result => {
console.log(result); // thenable success
});19、手写 Promise.prototype.finally
- Promise.prototype.finally:finally 用于在 Promise 成功或失败后都执行同一段回调,常用于清理逻辑,并且默认透传原来的结果或失败原因。
- 应用场景:
- 请求完成后关闭 loading。
- 异步任务完成后释放资源。
- 无论成功失败都需要执行收尾逻辑。
- 实现原理:
- finally 返回一个新的 Promise。
- 成功时执行回调,等待回调完成后继续返回原来的成功值。
- 失败时执行回调,等待回调完成后继续抛出原来的失败原因。
- 如果 finally 回调返回 rejected Promise 或抛出错误,则会用新的错误覆盖原结果。
- 注意事项:
- finally 的回调不接收成功值或失败原因。
- 默认情况下,finally 不改变原 Promise 的最终结果。
- 如果 finally 回调内部出错,则会影响后续状态。
详情
Promise.prototype.myFinally = function (callback) {
const onFinally = typeof callback === "function" ? callback : () => callback;
const P = this.constructor || Promise;
return this.then(
value => {
return P.resolve(onFinally()).then(() => value);
},
reason => {
return P.resolve(onFinally()).then(() => {
throw reason;
});
}
);
};
// 测试 Promise.prototype.finally
Promise.resolve("success")
.myFinally(() => {
console.log("finally 执行");
})
.then(result => {
console.log(result); // success
});
Promise.reject("error")
.myFinally(() => {
console.log("finally 也会执行");
})
.catch(error => {
console.log(error); // error
});
Promise.resolve("success")
.myFinally(() => {
return Promise.reject("finally error");
})
.catch(error => {
console.log(error); // finally error
});20、手写并发请求控制函数
- 并发请求控制:并发请求控制是指限制同一时间正在执行的异步任务数量,避免一次性发起过多请求导致浏览器、服务端或网络压力过大。
- 应用场景:
- 批量上传文件。
- 批量请求列表详情。
- 大量异步任务需要限制最大并发数。
- 实现原理:
- 维护当前正在执行的任务数量 activeCount。
- 维护下一个待执行任务的下标 currentIndex。
- 每次启动任务时增加 activeCount。
- 任务完成后减少 activeCount,并继续启动下一个任务。
- 所有任务完成后,按原始顺序 resolve 结果数组。
- 注意事项:
- 返回结果的顺序应和任务传入顺序一致。
- 任一任务失败时,可以直接 reject,也可以按业务改成收集失败结果。
- 最大并发数需要大于 0。
详情
function limitRequest(tasks, limit) {
if (!Array.isArray(tasks)) {
return Promise.reject(new TypeError("tasks must be an array"));
}
if (limit <= 0) {
return Promise.reject(new Error("limit must be greater than 0"));
}
return new Promise((resolve, reject) => {
const result = [];
let currentIndex = 0;
let activeCount = 0;
let finishedCount = 0;
if (tasks.length === 0) {
resolve([]);
return;
}
function runNext() {
while (activeCount < limit && currentIndex < tasks.length) {
const index = currentIndex;
const task = tasks[index];
currentIndex++;
activeCount++;
Promise.resolve()
.then(task)
.then(value => {
result[index] = value;
finishedCount++;
activeCount--;
if (finishedCount === tasks.length) {
resolve(result);
return;
}
runNext();
})
.catch(reject);
}
}
runNext();
});
}
// 测试并发请求控制函数
function createTask(value, delay) {
return () => {
return new Promise(resolve => {
setTimeout(() => {
console.log("完成任务:", value);
resolve(value);
}, delay);
});
};
}
const tasks = [
createTask(1, 1000),
createTask(2, 500),
createTask(3, 300),
createTask(4, 800),
];
limitRequest(tasks, 2).then(result => {
console.log(result); // [1, 2, 3, 4]
});21、手写 LRU 缓存
- LRU 缓存:LRU 是 Least Recently Used 的缩写,表示最近最少使用。当缓存容量满时,优先淘汰最久没有被访问的数据。
- 应用场景:
- 浏览器缓存。
- 接口数据缓存。
- 图片、文件、计算结果等有限容量缓存。
- 实现原理:
- 使用 Map 保存缓存数据,Map 会按照插入顺序保存 key。
- get 命中缓存时,先删除原 key,再重新 set,使它变成最新使用的数据。
- put 新增或更新数据时,也将该 key 放到最新位置。
- 当容量超过限制时,删除 Map 中第一个 key,也就是最久未使用的数据。
- 注意事项:
- get 和 put 都需要更新数据的使用顺序。
- Map 的 keys().next().value 可以获取最早插入的 key。
- 如果面试要求严格 O(1),也可以使用哈希表 + 双向链表实现。
详情
class LRUCache {
constructor(capacity) {
if (capacity <= 0) {
throw new Error("capacity must be greater than 0");
}
this.capacity = capacity;
this.cache = new Map();
}
get(key) {
if (!this.cache.has(key)) {
return -1;
}
const value = this.cache.get(key);
// 删除后重新插入,让当前 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) {
const oldestKey = this.cache.keys().next().value;
this.cache.delete(oldestKey);
}
}
}
// 测试 LRU 缓存
const lru = new LRUCache(2);
lru.put("a", 1);
lru.put("b", 2);
console.log(lru.get("a")); // 1
lru.put("c", 3); // 淘汰 b
console.log(lru.get("b")); // -1
console.log(lru.get("c")); // 3
lru.put("d", 4); // 淘汰 a
console.log(lru.get("a")); // -1
console.log(lru.get("d")); // 422、手写 compose 函数
- compose 函数:compose 用于把多个函数组合成一个函数,执行顺序通常是从右到左。
- 应用场景:
- 函数组合。
- Redux 中间件组合。
- 多个数据转换函数串联执行。
- 实现原理:
- 接收多个函数作为参数。
- 返回一个新的函数。
- 新函数执行时,从右到左依次执行每个函数。
- 上一个函数的返回值会作为下一个函数的参数。
详情
function compose(...fns) {
if (fns.length === 0) {
return value => value;
}
if (fns.length === 1) {
return fns[0];
}
return function (...args) {
let index = fns.length - 1;
let result = fns[index](...args);
while (--index >= 0) {
result = fns[index](result);
}
return result;
};
}
// 测试 compose 函数
const add = x => x + 1;
const double = x => x * 2;
const square = x => x * x;
const fn = compose(square, double, add);
console.log(fn(2)); // square(double(add(2))) = 3623、手写 pipe 函数
- pipe 函数:pipe 也用于函数组合,但执行顺序通常是从左到右。
- 应用场景:
- 数据处理流水线。
- 多个格式化函数按顺序执行。
- 将复杂转换拆成多个小函数组合。
- 实现原理:
- 接收多个函数作为参数。
- 返回一个新函数。
- 新函数执行时,先执行最左侧函数。
- 前一个函数的返回值作为后一个函数的入参。
详情
function pipe(...fns) {
if (fns.length === 0) {
return value => value;
}
if (fns.length === 1) {
return fns[0];
}
return function (...args) {
let result = fns[0](...args);
for (let i = 1; i < fns.length; i++) {
result = fns[i](result);
}
return result;
};
}
// 测试 pipe 函数
const add = x => x + 1;
const double = x => x * 2;
const square = x => x * x;
const fn = pipe(add, double, square);
console.log(fn(2)); // square(double(add(2))) = 3624、手写 Promise 重试函数
- Promise 重试函数:用于异步任务失败后按指定次数重新执行。
- 应用场景:
- 网络请求偶发失败后重试。
- 上传分片失败后重试。
- 第三方接口短暂不可用时重试。
- 实现原理:
- 接收一个返回 Promise 的函数。
- 执行失败后判断是否还有剩余重试次数。
- 有剩余次数则等待一段时间后再次执行。
- 次数耗尽后抛出最后一次错误。
详情
function sleep(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
async function retry(task, times = 3, delay = 0) {
let lastError;
for (let i = 0; i <= times; i++) {
try {
return await task();
} catch (error) {
lastError = error;
if (i === times) {
break;
}
if (delay > 0) {
await sleep(delay);
}
}
}
throw lastError;
}
// 测试 Promise 重试函数
let count = 0;
retry(
() => {
count++;
if (count < 3) {
return Promise.reject(new Error("请求失败"));
}
return Promise.resolve("请求成功");
},
3,
1000,
).then(result => {
console.log(result); // 请求成功
});注意事项
task必须是函数,不能直接传入已经执行过的 Promise。- 重试只适合幂等或可安全重复执行的任务。
- 生产中可以增加指数退避、最大延迟、取消控制等能力。
25、手写模板字符串渲染函数
- 模板字符串渲染函数:把模板中的占位符替换成数据对象中的值。
- 应用场景:
- 简单消息模板。
- 文案变量替换。
- 低复杂度模板引擎实现。
- 实现原理:
- 使用正则匹配
形式的占位符。 - 根据 key 从数据对象中取值。
- 支持通过点路径读取嵌套属性。
- 找不到值时返回空字符串或保留原占位符。
- 使用正则匹配
详情
function getValue(data, path) {
return path.split(".").reduce((result, key) => {
if (result == null) {
return undefined;
}
return result[key];
}, data);
}
function render(template, data) {
return template.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (match, key) => {
const value = getValue(data, key);
return value == null ? "" : String(value);
});
}
// 测试模板字符串渲染函数
const template = "你好,{{ user.name }},今天是 {{ date }}";
const result = render(template, {
user: {
name: "Tom",
},
date: "周三",
});
console.log(result); // 你好,Tom,今天是 周三注意事项
- 这种实现只适合简单变量替换,不支持表达式、循环和条件判断。
- 如果模板来自用户输入,需要注意 XSS 风险。
- 复杂场景应使用成熟模板引擎或框架能力。
26、手写带 once 和 off 的 EventEmitter
- EventEmitter:用于实现事件订阅、发布、取消订阅和一次性订阅。
- 应用场景:
- 组件通信。
- 插件系统事件通知。
- Node.js 事件模型理解。
- 实现原理:
- 使用 Map 保存事件名和监听函数集合。
on负责订阅事件。off负责移除监听函数。once包装原函数,执行一次后自动移除。emit按顺序执行当前事件的监听函数。
详情
class EventEmitter {
constructor() {
this.events = new Map();
}
on(eventName, listener) {
if (!this.events.has(eventName)) {
this.events.set(eventName, new Set());
}
this.events.get(eventName).add(listener);
return () => this.off(eventName, listener);
}
off(eventName, listener) {
const listeners = this.events.get(eventName);
if (!listeners) return;
listeners.delete(listener);
if (listeners.size === 0) {
this.events.delete(eventName);
}
}
once(eventName, listener) {
const wrapper = (...args) => {
this.off(eventName, wrapper);
listener(...args);
};
return this.on(eventName, wrapper);
}
emit(eventName, ...args) {
const listeners = this.events.get(eventName);
if (!listeners) return;
[...listeners].forEach(listener => {
listener(...args);
});
}
}
// 测试 EventEmitter
const emitter = new EventEmitter();
const unsubscribe = emitter.on("message", value => {
console.log("on:", value);
});
emitter.once("message", value => {
console.log("once:", value);
});
emitter.emit("message", "hello");
emitter.emit("message", "world");
unsubscribe();
emitter.emit("message", "again");注意事项
emit时复制监听函数集合,可以避免遍历过程中增删监听导致异常。on返回取消订阅函数,使用起来更方便。- 实际工程中还可以增加最大监听数量、错误事件和通配符事件。
27、手写 sleep 和 Promise 超时控制
sleep:返回一个在指定时间后完成的 Promise。- Promise 超时控制:给异步任务设置最大等待时间,超过后自动 reject。
- 应用场景:
- 延迟执行。
- 请求超时兜底。
- 动画步骤串行控制。
- 轮询间隔控制。
详情
手写 sleep
function sleep(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
}
async function run() {
console.log("start");
await sleep(1000);
console.log("end");
}手写超时控制
function withTimeout(promise, timeout, message = "timeout") {
let timer;
const timeoutPromise = new Promise((resolve, reject) => {
timer = setTimeout(() => {
reject(new Error(message));
}, timeout);
});
return Promise.race([promise, timeoutPromise]).finally(() => {
clearTimeout(timer);
});
}使用示例
withTimeout(fetch("/api/user"), 3000, "请求超时")
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error(error.message);
});注意事项
Promise.race只能让外层 Promise 超时,不一定能真正取消原任务。- 如果是
fetch,要真正取消请求应配合AbortController。 - 超时后要清理定时器,避免无用回调残留。
28、手写根据路径获取对象属性
- 根据路径获取对象属性:通过字符串路径读取对象中的深层属性。
- 应用场景:
- 表单字段读取。
- 配置项读取。
- 表格列字段映射。
- 安全访问嵌套对象属性。
- 实现原理:
- 将路径字符串转换成 key 数组。
- 从对象根节点开始逐层读取。
- 中间任意一层为空时返回默认值。
详情
function get(object, path, defaultValue) {
const keys = Array.isArray(path)
? path
: path.replace(/\[(\w+)\]/g, ".$1").split(".");
let result = object;
for (const key of keys) {
if (result == null) {
return defaultValue;
}
result = result[key];
}
return result === undefined ? defaultValue : result;
}
// 测试 get 函数
const data = {
user: {
profile: {
name: "Tom",
},
posts: [
{
title: "hello",
},
],
},
};
console.log(get(data, "user.profile.name")); // Tom
console.log(get(data, "user.posts[0].title")); // hello
console.log(get(data, "user.age", 18)); // 18注意事项
- 这里的实现只处理常见点路径和简单数组下标。
- 如果 key 本身包含
.,需要更复杂的路径解析规则。 - 读取属性时要避免执行不可信对象上的 getter 产生副作用。
29、手写根据路径设置对象属性
- 根据路径设置对象属性:通过字符串路径修改对象中的深层属性。
- 应用场景:
- 表单字段赋值。
- 配置项更新。
- 根据接口字段路径写入数据。
- 和路径读取函数配套使用。
- 实现原理:
- 将路径字符串转换成 key 数组。
- 从对象根节点开始逐层创建对象或数组。
- 到最后一层时写入目标值。
详情
function set(object, path, value) {
const keys = Array.isArray(path)
? path
: path.replace(/\[(\w+)\]/g, ".$1").split(".");
let current = object;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (i === keys.length - 1) {
current[key] = value;
return object;
}
const nextKey = keys[i + 1];
if (current[key] == null || typeof current[key] !== "object") {
current[key] = /^\d+$/.test(nextKey) ? [] : {};
}
current = current[key];
}
return object;
}
// 测试 set 函数
const data = {};
set(data, "user.profile.name", "Tom");
set(data, "user.posts[0].title", "hello");
console.log(data.user.profile.name); // Tom
console.log(data.user.posts[0].title); // hello注意事项
- 这个实现会直接修改原对象。
- 如果需要不可变更新,需要每一层都复制后再写入。
- 对不可信 path 要做限制,避免写入
__proto__等危险属性。
30、手写 pick 和 omit 函数
pick:从对象中挑选指定 key,返回新对象。omit:从对象中排除指定 key,返回新对象。- 应用场景:
- 表单提交前筛选字段。
- 删除敏感字段。
- 构造接口参数。
- 对对象做浅层字段裁剪。
详情
手写 pick
function pick(object, keys) {
return keys.reduce((result, key) => {
if (Object.prototype.hasOwnProperty.call(object, key)) {
result[key] = object[key];
}
return result;
}, {});
}手写 omit
function omit(object, keys) {
const keySet = new Set(keys);
const result = {};
Object.keys(object).forEach(key => {
if (!keySet.has(key)) {
result[key] = object[key];
}
});
return result;
}测试
const user = {
id: 1,
name: "Tom",
password: "123456",
};
console.log(pick(user, ["id", "name"])); // { id: 1, name: "Tom" }
console.log(omit(user, ["password"])); // { id: 1, name: "Tom" }注意事项
- 这里实现的是浅拷贝。
- 如果字段值是对象,结果中仍然共享同一个引用。
- 处理敏感数据时,后端仍应做字段过滤,不能只依赖前端。
31、手写 deepEqual 深度比较函数
深度比较用于判断两个值在结构和内容上是否相等,常见于缓存判断、表单脏检查和测试断言。
详情
实现思路
- 先用
Object.is处理基本类型、NaN、-0等特殊值。 - 如果任意一方不是对象,直接返回
false。 - 比较对象的 key 数量。
- 递归比较每个 key 对应的值。
基础实现
function deepEqual(a, b) {
if (Object.is(a, b)) {
return true;
}
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
return false;
}
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
for (const key of aKeys) {
if (!Object.prototype.hasOwnProperty.call(b, key)) {
return false;
}
if (!deepEqual(a[key], b[key])) {
return false;
}
}
return true;
}测试
console.log(deepEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } })); // true
console.log(deepEqual([1, 2, 3], [1, 2, 3])); // true
console.log(deepEqual({ a: 1 }, { a: "1" })); // false注意事项
- 这个版本没有处理循环引用。
- 没有特殊处理
Date、RegExp、Map、Set等对象。 - 面试中可以先写基础版本,再说明如何扩展边界情况。
32、手写 groupBy 数组分组函数
groupBy 用于按照指定规则把数组元素分组,常见于列表聚合、表格分组和数据统计。
详情
实现思路
- 遍历数组。
- 根据传入函数计算分组 key。
- 如果分组不存在,先初始化为空数组。
- 把当前元素放入对应分组。
基础实现
function groupBy(array, iteratee) {
return array.reduce((result, item, index) => {
const key = iteratee(item, index, array);
if (!Object.prototype.hasOwnProperty.call(result, key)) {
result[key] = [];
}
result[key].push(item);
return result;
}, {});
}支持字符串字段
function groupBy(array, iteratee) {
const getKey =
typeof iteratee === "function" ? iteratee : item => item?.[iteratee];
return array.reduce((result, item, index) => {
const key = getKey(item, index, array);
if (!Object.prototype.hasOwnProperty.call(result, key)) {
result[key] = [];
}
result[key].push(item);
return result;
}, {});
}测试
const users = [
{ name: "Tom", role: "admin" },
{ name: "Jack", role: "user" },
{ name: "Lucy", role: "admin" },
];
console.log(groupBy(users, "role"));输出:
{
admin: [
{ name: "Tom", role: "admin" },
{ name: "Lucy", role: "admin" },
],
user: [
{ name: "Jack", role: "user" },
],
}注意事项
- 对象 key 会被转换成字符串。
- 如果分组 key 可能是对象,可以考虑使用
Map。 - 真实业务中要明确空值、缺失字段和非法数据的处理规则。
33、手写数组去重函数
数组去重用于移除数组中的重复元素,常见实现包括 Set 去重、循环判断去重和按字段去重。
详情
使用 Set 去重
function unique(array) {
return [...new Set(array)];
}测试:
console.log(unique([1, 2, 2, 3, 3, 4])); // [1, 2, 3, 4]这种方式简洁,适合基本类型数组。
使用 includes 去重
function unique(array) {
const result = [];
for (const item of array) {
if (!result.includes(item)) {
result.push(item);
}
}
return result;
}对象数组按字段去重
function uniqueBy(array, key) {
const map = new Map();
for (const item of array) {
if (!map.has(item[key])) {
map.set(item[key], item);
}
}
return [...map.values()];
}测试:
const list = [
{ id: 1, name: "Tom" },
{ id: 1, name: "Tom again" },
{ id: 2, name: "Jack" },
];
console.log(uniqueBy(list, "id"));注意事项
Set对对象去重时比较的是引用地址。includes对NaN可以正确判断,但整体时间复杂度较高。- 对象数组去重要先明确保留第一项还是最后一项。
34、手写数组 chunk 分块函数
chunk 用于把数组按指定大小拆分成多个子数组,常见于分页处理、批量请求和任务分批执行。
详情
实现思路
- 按指定 size 遍历数组。
- 每次截取一段作为子数组。
- 把子数组放入结果数组。
基础实现
function chunk(array, size) {
if (!Array.isArray(array)) {
return [];
}
if (size <= 0) {
return [];
}
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
}测试
console.log(chunk([1, 2, 3, 4, 5], 2));
// [[1, 2], [3, 4], [5]]
console.log(chunk([1, 2, 3], 3));
// [[1, 2, 3]]reduce 实现
function chunk(array, size) {
return array.reduce((result, item, index) => {
const groupIndex = Math.floor(index / size);
if (!result[groupIndex]) {
result[groupIndex] = [];
}
result[groupIndex].push(item);
return result;
}, []);
}注意事项
size小于等于 0 时要明确返回结果或抛错。slice会返回浅拷贝,子数组中的对象仍然共享引用。- 用于批量请求时,还需要配合并发控制,而不只是分块。
35、手写 memoize 缓存函数
memoize 用于缓存函数调用结果,当相同参数再次调用时直接返回缓存结果,适合纯函数和重复计算场景。
详情
基础实现
function memoize(fn, resolver) {
const cache = new Map();
return function (...args) {
const key = resolver ? resolver(...args) : JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
const result = fn.apply(this, args);
cache.set(key, result);
return result;
};
}使用示例
function add(a, b) {
console.log("calculate");
return a + b;
}
const memoizedAdd = memoize(add);
console.log(memoizedAdd(1, 2)); // calculate 3
console.log(memoizedAdd(1, 2)); // 3,直接从缓存读取自定义 key
const getUser = memoize(
user => user.name,
user => user.id
);对象参数默认用 JSON.stringify 可能不稳定,也可能因为字段顺序、循环引用导致问题,所以可以提供 resolver 自定义缓存 key。
带缓存上限
function memoize(fn, maxSize = 100) {
const cache = new Map();
return function (...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
const value = cache.get(key);
cache.delete(key);
cache.set(key, value);
return value;
}
const result = fn.apply(this, args);
cache.set(key, result);
if (cache.size > maxSize) {
const firstKey = cache.keys().next().value;
cache.delete(firstKey);
}
return result;
};
}注意事项
- 只适合纯函数,函数结果依赖外部状态时不适合盲目缓存。
- 缓存会占用内存,需要考虑清理或最大容量。
- 参数是对象、函数、Symbol 时,key 生成策略要谨慎。
- 异步函数缓存还要考虑失败结果是否缓存、并发请求是否复用同一个 Promise。
36、手写 once 函数
once 用于把一个函数包装成只执行一次的函数,后续再次调用时不再执行原函数,常用于初始化、事件绑定和一次性提交等场景。
详情
基础实现
function once(fn) {
let called = false;
let result;
return function (...args) {
if (!called) {
called = true;
result = fn.apply(this, args);
}
return result;
};
}这里用闭包保存 called 和第一次执行结果。
使用示例
const init = once(function (name) {
console.log("init", name);
return `${name} done`;
});
console.log(init("app")); // init app,app done
console.log(init("page")); // app done第二次调用不会再执行原函数,会直接返回第一次的结果。
为什么要保留 this
const obj = {
count: 1,
add: once(function (num) {
this.count += num;
return this.count;
}),
};使用 fn.apply(this, args) 可以保留调用时的 this 和参数。
异步函数场景
const loadConfig = once(() => fetch("/config").then(res => res.json()));如果原函数返回 Promise,后续调用会复用第一次返回的 Promise。
注意事项
- once 是否返回第一次结果,要根据业务约定明确。
- 如果第一次执行失败,是否允许再次执行也要单独设计。
- 包装后的函数会通过闭包持有第一次结果,结果很大时要注意内存占用。
- once 和节流不同,once 是整个生命周期只执行一次。
37、手写固定高度虚拟列表核心逻辑
虚拟列表的核心思想是只渲染可视区域附近的数据,通过占位高度和偏移量模拟完整列表,从而减少 DOM 数量。
详情
核心参数
固定高度虚拟列表通常需要:
list:完整数据。itemHeight:每一项固定高度。containerHeight:容器高度。scrollTop:当前滚动距离。buffer:上下额外渲染数量。
计算可视范围
function getVirtualList({
list,
itemHeight,
containerHeight,
scrollTop,
buffer = 5,
}) {
const visibleCount = Math.ceil(containerHeight / itemHeight);
const start = Math.max(0, Math.floor(scrollTop / itemHeight) - buffer);
const end = Math.min(list.length, start + visibleCount + buffer * 2);
const offsetTop = start * itemHeight;
const totalHeight = list.length * itemHeight;
return {
start,
end,
offsetTop,
totalHeight,
visibleList: list.slice(start, end),
};
}渲染结构
<div class="container">
<div class="placeholder">
<div class="content">
<!-- visible items -->
</div>
</div>
</div>关键样式:
.container {
height: 400px;
overflow: auto;
}
.placeholder {
height: var(--total-height);
position: relative;
}
.content {
transform: translateY(var(--offset-top));
}使用示例
const state = getVirtualList({
list,
itemHeight: 40,
containerHeight: 400,
scrollTop: 1200,
});根据 state.visibleList 渲染真实 DOM,根据 totalHeight 撑开滚动条,根据 offsetTop 移动可视内容。
注意事项
- 这个实现适合固定高度列表。
- 动态高度列表需要额外记录每项高度和位置,复杂度更高。
- 滚动事件要避免频繁触发重渲染,可以结合
requestAnimationFrame。 - buffer 太小可能白屏,太大会增加渲染数量。
38、手写 JSONP 请求函数
JSONP 利用 <script> 标签不受同源策略限制的特点发起跨域请求,服务端返回一段函数调用代码,把数据作为参数传给前端回调函数。
详情
实现思路
- 生成全局唯一回调函数名。
- 把回调函数名拼到 URL 参数中。
- 创建
script标签并插入页面。 - 服务端返回
callback(data)。 - 成功或失败后清理 script 和全局函数。
基础实现
function jsonp(url, params = {}, callbackKey = "callback") {
return new Promise((resolve, reject) => {
const callbackName = `jsonp_${Date.now()}_${Math.random().toString(16).slice(2)}`;
const script = document.createElement("script");
const query = new URLSearchParams({
...params,
[callbackKey]: callbackName,
});
window[callbackName] = data => {
cleanup();
resolve(data);
};
script.onerror = () => {
cleanup();
reject(new Error("JSONP request failed"));
};
function cleanup() {
delete window[callbackName];
script.remove();
}
script.src = `${url}${url.includes("?") ? "&" : "?"}${query.toString()}`;
document.body.appendChild(script);
});
}使用示例
jsonp("https://example.com/api", { id: 1 }).then(data => {
console.log(data);
});服务端返回内容类似:
jsonp_123({ "name": "Tom" });局限性
- 只支持 GET 请求。
- 需要服务端配合返回函数调用。
- 错误处理能力弱。
- 有安全风险,等于执行了远程脚本。
注意事项
- 现代项目优先使用 CORS。
- JSONP URL 必须可信,不要加载不受信任的脚本。
- 回调函数名要避免冲突,并在完成后清理。
- 需要设置超时逻辑时,可以额外加
setTimeout。
39、手写并发请求调度器
并发请求调度器用于限制同一时间正在执行的异步任务数量,常见于批量上传、批量请求、爬取数据等场景。
详情
实现思路
- 维护一个任务队列。
- 记录当前正在执行的任务数量。
- 当运行数量小于最大并发数时,从队列取任务执行。
- 任务完成后减少运行数量,并继续调度下一个任务。
基础实现
class Scheduler {
constructor(limit) {
this.limit = limit;
this.running = 0;
this.queue = [];
}
add(task) {
return new Promise((resolve, reject) => {
this.queue.push({
task,
resolve,
reject,
});
this.run();
});
}
run() {
while (this.running < this.limit && this.queue.length > 0) {
const { task, resolve, reject } = this.queue.shift();
this.running++;
Promise.resolve()
.then(task)
.then(resolve, reject)
.finally(() => {
this.running--;
this.run();
});
}
}
}使用示例
const scheduler = new Scheduler(2);
const request = time => () =>
new Promise(resolve => {
setTimeout(() => {
console.log(time);
resolve(time);
}, time);
});
scheduler.add(request(1000));
scheduler.add(request(500));
scheduler.add(request(300));
scheduler.add(request(400));最多同时执行 2 个任务,某个任务完成后才会继续执行下一个。
函数式写法
function limitRun(tasks, limit) {
const scheduler = new Scheduler(limit);
return Promise.all(tasks.map(task => scheduler.add(task)));
}注意事项
- 任务函数应该返回 Promise 或普通值。
- 要把失败结果正确 reject 出去。
- 如果需要“失败不中断全部任务”,可以在任务内部 catch 后返回错误对象。
- 大量任务场景下可以增加取消、优先级、重试等能力。
