WBlog

wangzhiwei blog

0%

复习

闭包

闭包:是一个函数和其词法环境的组合,它允许函数访问其外部作用域的变量。
优点:可以创建私有变量、数据封装、实现柯里化等高级函数式编程
潜在问题:主要是内存泄漏(因为外部变量无法被回收)和循环中的变量引用问题。
避免内存泄漏的方法:在不需要时手动解除引用(= null),以及及时移除事件监听器。

问题:内存泄露

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
手动解除引用

function createLeakyClosure() {

let largeData = { // 创建一个非常大的数据对象};

return function () {
// ... use largeData
}
}

let myClosure = createLeakyClosure();
myClosure = null;

// Dom 事件监听器用的比较多的地方 要及时移除事件监听器

例如 react 中的 useEffect 中

return () => {
document.removeEventListener('click', handleClick);
}

函数柯里化

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const sumFn = (...args) => {
return args.reduce((a, b) => a + b);
}

const sortFn = (...args) => {
return args.sort((a, b) => a - b);
}

var currying = function (fn) {
const args = [];

return function result(...rest) {
if (rest.length === 0) {
//return fn.apply(this, args);
return fn(...args);
} else {
args.push(...rest);
return result;
}
}
}


currying(sortFn)(10)(2,7)(3)(4)(5)()

currying(sumFn)(1)(2)(3)(4)(5)()


// 应用

function curry(fn) {
return function curried(...args) {

debugger
// 如果参数够了,直接执行
if (args.length >= fn.length) {
return fn.apply(this, args);
}
// 参数不够,返回新函数继续收集参数
return function(...nextArgs) {
return curried.apply(this, args.concat(nextArgs));
};
};
}

// 使用示例
function sum(a, b, c, d) {
return a + b + c + d;
}

const curriedSum = curry(sum);

// 多种调用方式
curriedSum(1)(2)(3)(4); // 10


// API 请求配置
const request = curry((baseURL, endpoint, method, data) =>
fetch(`${baseURL}${endpoint}`, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
);

const api = request('https://api.example.com');
const getUser = api('/users');
const createUser = getUser('POST');

// 使用
createUser({ name: '张三', age: 25 });

类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

String.prototype.valueOf = function () {
return 'hello world';
}
Number.prototype.valueOf = function () {
return 123;
}


隐式类型转换会调用 valueOf() 和 toString() 方法.


console.log('[] == ![]', [] == ![]) // true

// [] == false
// [] == 0
// '' == 0

console.log('[] == []', [] == []) // false
console.log('{} == {}', {} == {}) // false




浅拷贝和深拷贝

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

var obj = {
a: 1,
b: 2
}

const obj2 = {
...obj
}
console.log(obj2)
//{a: 1, b: 2}
obj.a = 3
console.log(obj2)
// {a: 1, b: 2}


const arr = [{info: 1}]

const arr_clone = [...arr]

arr_clone[0].info = 2

console.log(arr)
// [{info: 2}]
console.log(arr_clone)
// [{info: 2}]


// 参考: https://www.kimi.com/share/19c8f33f-5102-8ea4-8000-00001e463675

特斯拉面试

1
2
3
4




https://max.book118.com/html/2025/1212/8036122044010021.shtm

https://blog.csdn.net/weixin_48350747/article/details/146115307

https://blog.csdn.net/2401_86373285/article/details/157695139

https://zhuanlan.zhihu.com/p/694840096


https://www.renrendoc.com/paper/510088715.html

手写一个hooks、手写实现 reduce、算法等
比如React Fiber、性能优化、工程化、网络安全等
面试题也都偏基础,比如 React Hook 基础、Redux、CSS、JS基础等

京东面试

js中的数据类型有哪些

Number 、String 、Boolean 、Null 、Undefined 、Symbol 、Object、Array

闭包

递归实现一个什么东西

深拷贝

类型判断

宏任务和微任务
宏任务
包含:setTimeout, setInterval, I/O 操作, UI 渲染, setImmediate
微任务
包含:Promise.then, process.nextTick, Object.observe(已废弃), MutationObserver

javascript 高级程序设计

https://juejin.cn/post/7077400026670399495

【有道云笔记】京东
https://share.note.youdao.com/s/GMVGkMIY