如下答案仅供参考,大部分题目都是考察灵活应用某些概念,不是生记硬背的javascript
QA:咱们都知道nodejs是单线程的,可是若是仔细看过Nodejs启动的进程,你仍是会发现实际上是有7个线程的。那么请问这7个线程都是负责哪些工做的?java
单线程只是由于执行js代码的只有一个,这个也叫做主线程。可是像GC这些事情是不会主线程上执行的,所以7个线程包括了:node
QA:了解过v8吗?v8里面也有一个叫作context的东西,每当咱们须要切换上下文的时候都会切换这个context,好比一个worker到另一个worker。可是切换上下文涉及到堆栈以及一些别的操做,能够说是比较费时的。那么v8里面是用了什么技术来优化这个上下文的切换?react
该题目考查对v8的基本了解程度,若是有看过v8的基本官方文档介绍的话,都知道V8利用大量缓存
来提升context切换的成本。正则表达式
参考:[译文]V8学习的高级进阶express
QA:作过Nodejs的进程状态信息监控吗?除了通常监控的CPU、内存以外,可能还须要监控event loop的平均调度时间,那么请问有什么办法能够统计到event loop的延迟时间呢?api
该题目考察对event loop的了解,若是知道event loop的原理,那么咱们能够主动加入一个定时器,而后就能够计算加入定时器的时间和回调函数被调用的时间差与定时器设置的超时时间之差,两者即是event loop的延迟时间。数组
伪代码实现:promise
const now = Date.now() // 若是须要精度更加精确,可使用process.hrtime.bigint()
setTimeout(() => {
const cost = Date.now() - now
const eventloopDelay = cost - 100 // 这个时间即是eventloop的延迟时间
}, 100)
复制代码
QA:说一下下面代码的打印结果:浏览器
const EventEmitter = require('events')
class EE extends EventEmitter {}
const yy = new EE()
console.log('测试开始')
yy.on('event', () => console.log('我是EventEmitter触发的事件回调'))
setTimeout(() => {
console.log('0 毫秒后到期的定时器回调1')
process.nextTick(() => console.log('我是0毫秒定时器1加塞的一个微任务'))
}, 0)
setTimeout(() => {
console.log('0 毫秒后到期的定时器回调2')
process.nextTick(() => console.log('我是0毫秒定时器2加塞的一个微任务'))
}, 0)
setImmediate(() => console.log('immediate 当即回调'))
process.nextTick(() => console.log('process.nextTick 的第一次回调'))
new Promise((resolve) => {
console.log('我是promise')
}).then(() => {
yy.emit('event')
process.nextTick(() => console.log('process.nextTick 的第二次回调'))
console.log('promise 第一次回调')
})
.then(() => console.log('promise 第二次回调'))
console.log('测试结束?')
复制代码
该题的解析参考:js语言中那些让你抓狂又容易混淆的概念(建议收藏)
QA:说一下当请求:curl 127.0.0.1:3000/api/test1
的时候下面代码的打印结果:
const express = require('express')
const app = express()
const sleep = (mseconds) => new Promise((resolve) => setTimeout(() => {
console.log('sleep timeout...')
resolve()
}, mseconds))
app.use(async (req, res, next) => {
console.log('I am the first middleware')
const startTime = Date.now()
console.log(`================ start ${req.method} ${req.url}`, { query: req.query, body: req.body });
next()
const cost = Date.now() - startTime
console.log(`================ end ${req.method} ${req.url} ${res.statusCode} - ${cost} ms`)
})
app.use((req, res, next) => {
console.log('I am the second middleware')
next()
console.log('second middleware end calling')
})
app.get('/api/test1', async(req, res, next) => {
console.log('I am the router middleware => /api/test1')
await sleep(2000)
res.status(200).send('hello')
})
app.listen(3000)
console.log('server listening at port 3000')
复制代码
该题的解析参考:js语言中那些让你抓狂又容易混淆的概念(建议收藏)
QA:请将下面代码使用修饰器的功能,将全部公共部分抽象出一个修饰函数,以即可以简单使用在各个请求之中
class allApisClass {
async fetchGoods(params) {
Toast.loading('请稍候...')
try {
const info = await fetchHandle(fetchGoods, params)
Toast.close()
return info
} catch (err) {
Toast.close()
Toast.error(err.msg)
}
}
}
复制代码
好比抽象后的修饰器是:decorator,那么我能够这么简单地使用:
class allApisClass {
@decortor()
async fetchGoods(params){
const info = await fetchHandle(fetchGoods, params)
return info
}
}
复制代码
请实现对应的修饰器函数:
function decorator(target, propertyKey, descriptor) {
}
复制代码
该题目的解析参考:你觉得装饰器那么容易学吗?
QA:你以为使用react框架的时候,用componentWillUnMount来统计访问时间靠谱吗?为何?若是是你作,你会怎么实现?
统计页面访问时间是从页面渲染完成开始计时到页面关闭,若是使用componentWillUnMount来统计页面关闭的时间是统计不到的,由于当页面关闭时并不会触发这个事件,该事件可以被触发存在的前提是页面仍然在浏览器中显示,因此是不靠谱的。监听页面关闭的只能使用原生的unload
事件。
QA:如何快速统计字符串某个字符出现的次数?如何使用正则表达式快速计算出现次数最多的字符?
第一个问题的答案是利用split
:
str.split('a').length - 1
复制代码
第二个问题的答案是利用正则表达式的捕捉组概念:/(\w)\1+/g
const str = 'fhsdfhdsofsifjisffs'
const arr = str.split(''); //把字符串转换为数组
//首先进行排序,这样结果会把相同的字符放在一块儿,而后再转换为字符串
const str = arr.sort().join('')
const value = ''
const index = 0
//匹配字符,且重复这个字符,重复次数至少一次
const re = /(\w)\1+/g
str.replace(re, function ($0, $1) {
console.log($0, $1)
//若是index保存的值小于$0的长度就进行下面的操做
if (index < $0.length) {
index = $0.length;
value = $1;
}
})
复制代码