在 JavaScript 中,代碼塊、函數(shù)或模塊為變量創(chuàng)建作用域。例如 if
代碼塊為變量 message
創(chuàng)建作用域:
if (true) { const message = 'Hello'; console.log(message); // 'Hello' } console.log(message); // throws ReferenceError
在 if
代碼塊作用域內(nèi)可以訪問 message
。但是在作用域之外,該變量不可訪問。
好的,這是作用域的簡短介紹。如果你想了解