js手写题


高级应用题

写一个函数实现防抖(debounce)

1
2
3
4
5
6
7
8
9
10
// 防抖函数
function myDebounce(fn,delay) {
let timer = null;
return function (...args) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this,args)
}, delay);
}
}

如何实现一个简单的Promise?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class mySimplePromise{
constructor(exector){
this.state = 'pending' // 初始状态
this.value = undefined // 存储结果值或失败原因
this.onresolvedCallbacks = [] // 成功的回调队列
this.onRejectedCallbacks = [] // 失败的回调队列

const resolve = (value) =>{
if (this.state == 'pending') {
this.state = 'resolved'
this.value = value
this.onresolvedCallbacks.forEach(fn => fn());
}
}

const reject = (reson) =>{
if (this.state == 'pending') {
this.state = 'rejected'
this.value = reson
this.onRejectedCallbacks.forEach(fn => fn());
}
}

exector(resolve,reject)
}

then(onResolved,onRejected){
if (this.state == 'resolved') {
resolve(onResolved)
}
if (this.state == 'rejected') {
reject(onRejected)
}
if (this.state == 'pending') {
this.onresolvedCallbacks.push(() => onResolved(this.value))
this.onRejectedCallbacks.push(() => onRejected(this.value))
}
}
}

const promise1 = new myPromise((resolve,reject) =>{
resolve('成功')
})

const promise2 = new myPromise((resolve,reject) =>{
setTimeout(() => {
resolve('延迟成功')
}, 2000);
})

promise1.then(res =>{
console.log(res); //立即输出‘成功’
})

promise2.then(res =>{
console.log(res); //延迟2s输出‘延迟成功’
})

写一个函数实现柯里化(curry)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function myCurry(fn) {
return function curried(...args) {
// 检查当前参数数量是否达到原函数需要的参数数量
if (args.length >= fn.length) {
// 参数足够,直接执行原函数
return fn.apply(this,args)
}else{
// 参数不足,返回一个新函数继续收集参数
return function (...args2) {
// 递归调用 curried,合并已有参数和新参数
return curried.apply(this,args.concat(args2))
}
}
}
}

function add(a,b,c) {
return a+b+c
}

const curriedAdd = myCurry(add)

console.log(curriedAdd(1)(2)(3));
console.log(curriedAdd(1,2)(3));
console.log(curriedAdd(1,2,3));

写一个函数实现深度克隆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function myDeepClone(obj) {
// 基本数据类型
if (obj == null || typeof obj !== 'object') {
return obj
}

// 处理普通对象
if (typeof obj === 'object') {
const cloned = {}
for (const key in obj) {
if (obj.hasOwnProperty(key)){
cloned[key] = myDeepClone(obj[key])
}
}
return cloned
}

// 处理数组
if (obj instanceof Array) {
return obj.map(item => myDeepClone(item))
}

// 处理 Date 对象
if (obj instanceof Date) {
return new Date(obj.getTime());
}
}

const original = {
name: "John",
age: 30,
hobbies: ["reading", "gaming"],
profile: {
birthday: new Date('1990-01-01'),
address: { city: "New York" }
}
};

const cloned = myDeepClone(original);

// 遇到对象 → 创建空对象 {}
// 拷贝 name: "John" → 基本类型,直接赋值
// 拷贝 age: 30 → 基本类型,直接赋值
// 拷贝 hobbies: ["reading", "gaming"] → 数组,递归拷贝每个元素
// 拷贝 profile: {...} → 对象,递归进入
// 拷贝 birthday: Date → 创建新的 Date 对象
// 拷贝 address: { city: "New York" } → 对象,继续递归