2 回答

TA貢獻2051條經驗 獲得超10個贊
根據文檔- It's important to point out that the hoisting will affect the variable declaration, but not its value's initialization. The value will be indeed assigned when the assignment statement is reached.
變量instructor將被提升到函數的頂部displayInstructor,但是它的值將在到達語句時分配var instructor = "Loser";。該return語句在執行實際賦值代碼之前使用,此時變量instructor為undefined.
function displayInstructor(){
console.log(instructor) // undefined
return instructor;
var instructor = "Loser";
}
console.log(displayInstructor());
相反,首先分配值,然后返回變量。
function displayInstructor() {
var instructor = "Loser";
return instructor;
}
console.log(displayInstructor());
添加回答
舉報