✍️述
文章记录笔试过程中遇到的有趣的题目。
📖1. 查找字符串中重复最多的字符
查找字符串中重复最多的字符,并返回重复次数「数组中出现次数最多的元素及出现的次数」
// solution-1
function calcRepeats(str) {
var counts = {}
var arr = str.split('')
for (var i = 0; i < arr.length; i += 1) {
if (counts.hasOwnProperty(arr[i])) {
counts[arr[i]] += 1
} else {
counts[arr[i]] = 1
}
}
var ovalues = Object.keys(counts).map(function (item) { return counts[item] })
var find = function (value) {
var res = {}
Object.keys(counts).map(function (item) {
if (counts[item] === value) {
res[item] = value
}
})
return res
}
var maxcount = Math.max.apply(null, ovalues)
return find(maxcount)
}
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
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
📖2. EventLoop异步机制「单线程」
javascript单线程,特有的EventLoop”简直“有趣 Link
下面这个题要想做对,需要了解几个核心概念
「MacroTaskQueue, MicroTaskQueue, StackQueue」
原则:
- 首先执行全局
script - 微任务执行优先于宏任务
- 队列FIFO
- 微任务队列清空后开始从宏任务队列读取,遇到微任务添加至微任务队列
- 3个队列为空即为结束
console.log(1);
setTimeout(() => {
console.log(2);
Promise.resolve().then(() => {
console.log(3)
});
});
new Promise((resolve, reject) => {
console.log(4)
resolve(5)
}).then((data) => {
console.log(data);
})
setTimeout(() => {
console.log(6);
})
console.log(7);
// result
1
4
7
5
2
3
6
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
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
📖3. Debounce & Throttle 函数
Javascript函数防抖「debounce」和节流「throttle」Link
// ========================
// debounce实现 {From 薄荷前端}
// ========================
//模拟一段ajax请求
function ajax(content) {
console.log('ajax request ' + content)
}
function debounce(fun, delay) {
return function (args) {
let that = this
let _args = args
clearTimeout(fun.id)
fun.id = setTimeout(function () {
fun.call(that, _args)
}, delay)
}
}
let inputb = document.getElementById('debounce')
let debounceAjax = debounce(ajax, 500)
inputb.addEventListener('keyup', function (e) {
debounceAjax(e.target.value)
})
// ========================
// throttle实现 {From 薄荷前端}
// ========================
function throttle(fun, delay) {
let last, deferTimer
return function (args) {
let that = this
let _args = arguments
let now = +new Date()
if (last && now < last + delay) {
clearTimeout(deferTimer)
deferTimer = setTimeout(function () {
last = now
fun.apply(that, _args)
}, delay)
}else {
last = now
fun.apply(that,_args)
}
}
}
let throttleAjax = throttle(ajax, 1000)
let inputc = document.getElementById('throttle')
inputc.addEventListener('keyup', function(e) {
throttleAjax(e.target.value)
})
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
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
📖4. Array相关的操作函数
- slice
- splice
- indexOf
- froEach
- join
- concat
- sort
- fill
- reverse
- push
- pop
- shift
- unshift
- map
- filter
- reduce
- some
- every
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
📖5. Object & null 的判断
// 直接使用if来判断
// 使用typeof来判断
1
2
3
4
2
3
4
📖5‘. JS原型链
/**
* prototype
* __proto__
* Function
* Object
*/
function Animal(name) {
this.name = name
}
Animal.prototype.superSpeak = function () {
console.log('say some super thing!')
}
function Dog() {
this.name = '狗'
}
Dog.prototype = new Animal()
const doggie = new Dog()
// doggie.superSpeak()
// 该函数创建的对象.__proto__ === 该函数.prototype
console.info(doggie.__proto__ === Dog.prototype)
// Animal -> Function -> Object -> null
console.info(Animal.__proto__.__proto__.__proto__) // null
// 该函数.prototypr.constructor === 该函数.prototype的对应函数
console.info(Dog.prototype.constructor === Animal) // true
// 函数独有prototype,而函数又是对象,所以又包含__proto__ constructor, 对象只包含__proto__ constructor
// __proto__串起来的就是圆形链,顶部是null,万物从混沌而起
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
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