變數提升驗證

lazysunday發表於2018-07-07

console.log(1,a); //變數
console.log(2,b); //函式
console.log(3,c); //函式體內定義
console.log(4,e); //函式
console.log(9,d); //變數未定義

var a = 'window';
var d = 'inWindow';
function b(){
    console.log(5,d);
    var c = 'funcVar'
    var d = 'inFunctionB';
    console.log(6,d);
}
var e = function(){
}
console.log(7,d);
b();
console.log(8,d);

複製程式碼

預想結果:

 1 undefined
 2 function
 3 undefined
 4 undefined
 9 undefined
 7 'inWindow'
 5 undefined
 6 'inFunctionB'
 8 'inWindow'
複製程式碼

chrome控制檯

1 undefined
2 ƒ b(){
    console.log(5,d);
    var c = 'funcVar'
    var d = 'inFunctionB';
    console.log(6,d);
}
3 報錯 Uncaught ReferenceError: c is not defined
4 undefined
9 undefined
7 "inWindow"
5 undefined
6 "inFunctionB"
8 "inWindow"
複製程式碼

nodejs中

node中
1 undefined
2 [Function: b]
3 導致異常無響應
4 undefined
9 undefined
7 'inWindow'
5 undefined
6 'inFunctionB'
8 'inWindow'複製程式碼


相關文章