笔试题
题目1:编写一个 People 类,使其的实例具有监听事件、触发事件、解除绑定功能。(实例可能监听多个不同的事件,也可以去除监听事件)
class People {
constructor(name) {
this.name = name
this.eventHandle = {}
}
// TODO: 请在此处完善代码
on(event, fn) {
if (event in this.eventHandle) {
this.eventHandle[event].push(fn)
} else {
this.eventHandle[event] = [fn]
}
}
emit(event, arg) {
this.eventHandle[event].forEach(fn => {
fn(arg)
})
}
off(event, fn) {
this.eventHandle[event] = this.eventHandle[event].filter(f => f !== fn)
}
sayHi() {
console.log(`Hi, I am ${this.name}`)
}
}
/* 以下为测试代码 */
const say1 = (greeting) => { console.log(`${greeting}, nice meeting you.`) }
const say2 = (greeting) => { console.log(`${greeting}, nice meeting you, too.`) }
const jerry = new People('Jerry')
jerry.sayHi() // => 输出:'Hi, I am Jerry'
jerry.on('greeting', say1)
jerry.on('greeting', say2)
jerry.emit('greeting', 'Hi') // => 输出:'Hi, nice meeting you.' 和 'Hi, nice meeting you, too'
jerry.off('greeting', say1)
jerry.emit('greeting', 'Hi') // => 只输出:'Hi, nice meeting you, too'
题目2:完成 sleep 函数,可以达到下面的效果:
const sleep = (duration) => {
// TODO
return new Promise(resolve => {
setTimeout(resolve, duration)
})
}
const anyFunc = async () => {
console.log("123") // 输出 123
await sleep(300) // 暂停 300 毫秒
console.log("456") // 输出 456,但是距离上面输出的 123 时间上相隔了 300 毫秒
}
anyFunc()
题目3:完成 deepGet 函数,给它传入一个对象和字符串,字符串表示对象深层属性的获取路径,可以深层次获取对象内容:
const deepGet = (obj, prop) => {
// TODO: 在此处完善代码
let result = obj
const keyArray = prop.split('.')
for (let i = 0; i < keyArray.length; i++) {
if (result) {
const key = keyArray[i]
const index = key.indexOf('[');
if (index === -1) { // 对象
result = result[key] ? result[key] : undefined
} else { // 数组
const arrayName = key.slice(0, index)
const arrayIndex = key.slice(index + 1, -1)
result = result[arrayName] ? result[arrayName][arrayIndex] : undefined
}
} else {
break;
}
}
console.log(result);
return result
}
/** 以下为测试代码 */
deepGet({ school: { student: { name: 'Tomy' }, }, }, 'school.student.name') // => 'Tomy'
deepGet({ school: { students: [{ name: 'Tomy' }, { name: 'Lucy' },], } }, 'school.students[1].name') // => 'Lucy' // 对于不存在的属性,返回 undefined
deepGet({ user: { name: 'Tomy' } }, 'user.age') // => undefined
deepGet({ user: { name: 'Tomy' } }, 'school.user.age') // => undefined
题目4:完成 combo 函数。它接受任意多个单参函数(只接受一个参数的函数)作为参数,并且返回一个函数。
它的作为用:使得类似 f(g(h(a))) 这样的函数调用可以简写为 combo(f, g, h)(a)。
const combo = (...fns) => {
// TODO: 请在此处完善代码
// 反转数组中元素的顺序
fns.reverse()
return (x) => {
fns.forEach(fn => {
x = fn(x)
})
console.log(x)
}
}
/* 以下为测试代码 */
const addOne = (a) => a + 1
const multiTwo = (a) => a * 2
const divThree = (a) => a / 3
const toString = (a) => a + ''
const split = (a) => a.split('')
split(toString(addOne(multiTwo(divThree(666))))) // => ["4", "4", "5"]
const testForCombo = combo(split, toString, addOne, multiTwo, divThree)
testForCombo(666) // => ["4", "4", "5"]