1.?預(yù)編譯
1. 創(chuàng)建了ao對象
2. 找形參和變量的聲明作為ao對象的屬性名 值是undefined
3. 實(shí)參和形參相統(tǒng)一
4. 找函數(shù)聲明會覆蓋變量的聲明
Function fn(a,c){
Console.log(a) ?// function a(){}
Var a = 123
Console.log(a) ?// 123
Console.log(c) ???// function c(){}
Function a(){}
If(false){
Var d = 678
}
Console.log(d) ?//undefined
Console.log(b) ?//undefined
Var b = function(){}
Console.log(b) ?// function (){}
Function c(){}
Console.log(c) ?// function c(){}
}
2.?防抖函數(shù)(用于echarts圖,提升性能)
典型案例:(輸入搜索)輸入結(jié)束后n秒才進(jìn)行搜索,n秒內(nèi)又輸入的內(nèi)容,就重新計(jì)算時(shí),解決搜索的bug
Function debounce(delay,callback){
Let timer
Return function(value){
clearTimeout(timer)
timer = setTimeout(function(){
Callback(value)
},delay)
}
}
Function fn(value){
Console.log(value)
}
3.節(jié)流函數(shù):當(dāng)觸發(fā)事件的時(shí)候,保證一段時(shí)間內(nèi)只調(diào)用一次事件處理函數(shù)
Function Thor(fun,wait){
Let tinerOut
Return function(){
If(!timerOut){
timerOut = setTimeout(function(){
Func()
timerOut = unll
},wait)
}
}
}
Function handle({
Console.log(Math.random)
})
Document.getElementById(‘button’).onclick = thro(handle,2000)
3.?js事件循環(huán)機(jī)制(調(diào)用棧-微任務(wù)隊(duì)列-消息隊(duì)列)
Js中的異步操作,比如fetch, setTimeout, setInterval 壓入到調(diào)用棧中的時(shí)候里面的消息會進(jìn)入到消息隊(duì)列中去,消息隊(duì)列中,會等到調(diào)用棧清空后再執(zhí)行。
Eg1:
Function fn1(){
Console.log(1)
}
Function fn2(){
Console.log(2)
Fn1()
Console.log(3)
}
Fn2()
輸出結(jié)果:2,1,3
Eg2:
Function fn1(){
Console.log(1)
}
Function fn2(){
setTimerout(()=>{
Console.log(2)
},0)
Fn1()
Console.log(3)
}
Fn2()
輸出結(jié)果:1,3,2
Promise, async, await的異步操作的時(shí)候會加入到微任務(wù)中去,會在調(diào)用棧清空的時(shí)候立即執(zhí)行,調(diào)用棧中加入的微任務(wù)會立馬執(zhí)行。
Eg:
Var p = new Promise(resolve?=> {
Console.log(4)
Resolve(5)
})
Function fn1(){
Console.log(1)
}
Function fn2(){
setTimerout(()=>{
Console.log(2)
},0)
Fn1()
Console.log(3)
P.then(resolve?=>{
Console.log(resolve)
})
}
Fn2()
輸出結(jié)果:4,1,3,5,2