# 一面
- 自我介绍,聊项目
- js数据类型
- 原型继承和原型链
- 事件循环机制,绑定的事件在哪个环节执行
- css水平垂直居中
- css实现扇形
- vue的diff算法
- http请求头都有哪些,都有什么作用
- 前端用到的缓存,知道多少说多少
- 读程序,聊函数提升
var a = 1;
function b() {
a = 10;
return;
function a() { }
}
b();
console.log(a);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- promise.all 实现
function all(promises) {
return new Promise((resolve, reject) => {
const result = [];
for (let i = 0, l = promises.length; i < l; i++) {
const p = promises[i];
p.then(resp => {
result.push(resp);
if (result.length === l) {
resolve(result);
}
}).catch(err => {
reject(err);
})
}
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 用数组的reduce方法实现map方法
Array.prototype._map_by_reduce = function _map_by_reduce(callback, context) {
const array = this;
if (array.length === 0) {
return [];
}
array.reduce((pre, cur, index, array) => {
callback.call(context, cur, index, array);
}, array[0])
};
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 二面
- 自我介绍,聊项目,聊前端制定规范的细节
- 登录和退出登录的原理,为什么后来用storage来存token
- 前端安全
- 聊 React 类组件和函数组件创建组件的区别
- 聊 JSX
- 聊 React Hooks 原理
- 读程序
function Foo() {
this.getName = function(){ alert(1); };
return this;
}
Foo.getName = function() { alert(2); };
Foo.prototype.getName = function(){ alert(3); };
var getName = function() { alert(4); };
function getName(){ alert(5); };
Foo.getName(); // 2
getName(); // 4
Foo().getName(); // 1
getName(); // 1
new (Foo.getName)(); // 2
(new Foo()).getName(); // 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- 写代码:实现一个 fetchLimit
function get(url) : Promise<string> {}
function fetchLimit(urls, limit, timeout): Promise<string[]> {
}
1
2
3
2
3
function get(url) {
return Promise.resolve(url);
}
async function fetch(url, timeout) {
const req = get(url);
const timer = new Promise((resolve, reject) => {
setTimeout(() => {
reject('timeout');
}, timeout)
})
return new Promise.race([req, timer]);
}
// 没写完
function fetchLimit(urls, limit, timeout) {
return new Promise((resolve, reject) => {
const len = urls.length;
const result = new Array(len);
let count = 0;
function request() {
const index = len - urls.length - 1;
if (urls.length === 0) {
return;
}
const url = urls.shift();
fetch(url, timeout).then(resp => {
result[index] = resp;
if (++count === len) {
resolve(result);
} else {
request();
}
}).catch(err => {
reject(err);
})
}
for (let i = 0; i < limit; i++) {
request();
}
})
}
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
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