<aside> 💡 Javascript 是一個單執行緒的程式 (Single Thread) 單執行緒式作業系統的運行概念 單執行緒就是只一次只能做一件事情 多執行緒可以同時執行 而單執行緒是針對系統的執行
</aside>
<aside> 💡 非同步是針對程式語言本身
</aside>
舉例
function eatBreakfast(){
console.log('吃早餐');
};
function callSomeone(someone){
console.log('打給'+ someone);
};
function washingPlate(){
console.log('洗餐盤');
};
function doWork(){
var auntie = "漂亮阿姨";
}
doWork();
同步執行的情況下
eatBreakfast()
callSomeone(someone)
washingPlate()
<aside> 💡 非同步執行方式,專有名詞叫event queue 事件佇列
</aside>
舉例
function eatBreakfast(){
console.log('吃早餐');
};
function callSomeone(someone){
console.log('打給'+ someone);
setTimeout(function(){ ====>非同步行為先移到事件佇列
console.log(someine + '回電');
}, 3000);
};
function washingPlate(){
console.log('洗餐盤');
};
function doWork(){
var auntie = "漂亮阿姨";
}
doWork();
同步執行的情況下
eatBreakfast()
callSomeone(someone)
washingPlate()
fn()
function fn(params) {
if(1a) {
a = 2;
}
}
var a = 1 ;
console.log(a);
//創造階段
function fn(params) {
if(1a) {
a = 2;
}
}
var a
//執行
fn()
a = 1
console.log(a);