阿里前端笔记

人生第一次BAT级别面试经验,还是笔试,总共三道题目,特此记录!

第一题:

1
2
3
4
5
6
7
8
9
10
/**
* 说明:获取数组中的最大数
* 输入:数组,如[1, 13, 25, -2, 47]
* 输出:最大数 47
*/
function getMaxFromArr(arr) {
/*功能实现*/
return Math.max.apply(null, arr)
}

第一题没啥好说的,就是apply的技巧用法。

第二题

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
/**
* 说明:生成一个指定长度(默认6位)的随机字符,随机字符包含小写字母和数字
* 输入:输出随机字符长度,无输出默认6位
* 输出:随机字符,如'6jbi0v'
*/
function idGenerator() {
/*功能实现*/
const num = arguments[0] || 6
const words = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
const arr = [words, numbers]
let result = ''
for (let i = 0; i < num; i++) {
const random = Math.floor(Math.random() * 2)
if (random === 0) {
const wordsRandom = Math.floor(Math.random() * 26)
result += arr[random][wordsRandom]
} else {
const numbersRandom = Math.floor(Math.random() * 10)
result += arr[random][numbersRandom]
}
}
return result
}

第二题考察点也就是生成随机固定范围的数,没什么难度。

第三题

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
/**
* 说明:实现类似双向绑定的功能
* 输入:const data = {
* a: 1,
* b: 2,
* c: {d: 3}
* }
* observe(data)
* 输出:data.a = 4 // a has changed to 4
* data.c.d = 5 // d has changed to 5
*/
function observe(data) {
if (!data && Object.prototype.toString.call(data) !== '[object Object]') return
for (key in data) {
defineReactive(data, key, data[key])
}
}
function defineReactive(data, key, value) {
observe(value)
Object.defineProperty(data, key, {
enumerable : true,
configurable : false,
get: function() {
return value
},
set: function(newValue) {
console.log(`${key} changed to ${newValue}`)
value = newValue
}
})
}
const data = {a: 1, b: 2, c: {d: 3}}
observe(data)
data.a = 4 // a has changed to 4
data.c.d = 5 // d has changed to 5

这题应该是三题之中的难题了,难度也还好,就是考察面试者的知识面知不知道Object.defineProperty和利用getter和setter做数据劫持。

以上就是阿里笔试的一次经历,菜鸟真的是瑟瑟发抖。。

菜鸟学习笔记,如有不对,还希望高手指点。如有造成误解,还希望多多谅解。

著作权归作者所有。
商业转载请联系作者获得授权,非商业转载请注明出处