块作用域使用 let
填空题 简单
题目
Fill in 空白所以变量是 scoped 到如果块 仅.
代码
if (true) {
___ message = "inside";
console.log(message);
}
// console.log(message); // ReferenceError
提示
两者都 let and const 是块-scoped, 但 var 是不.
解释
let (and const) 是块-scoped — 它们仅 exist 在内 nearest curly-brace 块. var 是函数-scoped, 所以它 会 leak 之外如果块. Using let 消息 confines 它到 如果块, and referencing 它之外 throws ReferenceError.
相关资源
相关课程
- 作用域 and Visibility
学习关于作用域 and 变量 visibility.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。