本以为自己面试准备的很好了 二面给我打脸了 基础不够扎实 知道概念 但一旦去写一些实例的时候 就只能说出个大概 手撕函数方面也只知道大概思路 真的写 还是会出现各种纰漏 1.防抖节流 应用场景 手写 手写出了问题
function debounce(fn,delay){
let timer;
return function(fn,delay){
if(timer){
clearTime(timer)
}
timer=setTimeout(()=>{
fn.apply()},delay)
}
}
function throttle(fn,delay){
let flag=true;
return function(){
if(flag){
flag=false;
setTimeOut(()=>{
fn.apply()
flag=true
},delay)
}
}
}
2.手写instanceof
//a是实例 b是构造函数
function Myinstanceof(a,b){
if(a._proto_==null){
return false;
}
if(a._proto_==b.prototype){
return true;
}elseg
Myinstanceof(a._proto_,b)
}
}
3.检测微任务与宏任务的理解
console.log('1');
setTimeout(function() {
console.log('2');
process.nextTick(function() {
console.log('3');
})
new Promise(function(resolve) {
console.log('4');
resolve();
}).then(function() {
console.log('5')
})
})
process.nextTick(function() {
console.log('6');
})
new Promise(function(resolve) {
console.log('7');
resolve();
}).then(function() {
console.log('8')
})
setTimeout(function() {
console.log('9');
process.nextTick(function() {
console.log('10');
})
new Promise(function(resolve) {
console.log('11');
resolve();
}).then(function() {
console.log('12')
})
})
首先是执行完全部的同步任务 promise函数里的部分也是属于同步任务 只有回调是属于微任务 所以是先是1 7 然后到任务队列 会先执行完所有的微任务 里面的微任务有nexttick和 promise的then回调 所以是 1 7 6 8 然后开始执行第一个宏任务 变成 1 7 6 8 2 5,期间加了两个微任务到任务队列,会再清空微任务再执行下一个宏任务 变成 1 7 6 8 2 4 3 5 然后是下一个宏任务 1 7 6 8 2 4 3 5 9 11 然后和上一步相同 1 7 6 8 2 4 3 5 9 11 10 12
还有一道我暂时想不起来了 可恶 先记到这为止吧