[ES6] 04. The let keyword -- 2 Fiald case
ES6 -- The 04 case keyword Let
2023-09-14 09:00:55 时间
Fiald case 1: let can work in it's block
{ let a = 10; var b = 1; } a // ReferenceError: a is not defined. b //1
Case 2: Let has no "Hosting" Problem
function do_something() { console.log(foo); // ReferenceError let foo = 2; }
function do_something() { console.log(foo); //undefined var foo = 2; } //hosting: function do_something() { var foo = undefined; console.log(foo); foo = 2; }
Case 3: let不允许在相同作用域内,重复声明同一个变量: In a block, you can only define one variable one time.
// 报错 { let a = 10; var a = 1; } // 报错 { let a = 10; let a = 1; }
But if you do something like:
function f1() { let n = 5; if (true) { let n = 10; } console.log(n); // 5 }
There is no problem! becuase 5 and 10 are in different block scope.
相关文章
- es6学习
- [ES6] The Iterator Protocol
- [ES6] Function Params
- [ES6] Converting an array-like object into an Array with Array.from()
- [ES6] 15. Generators -- 2
- [ES6] 08. Destructuring Assignment -- 1
- js es6 Iterator
- es6小记
- [ES6] 16. Object Enhancements
- [ES6] 09. Destructuring Assignment -- 2
- Es6系列之destructuring assignments
- js es6 delete
- ES6学习之路1----变量的声明
- ES6学习之路5----数组方法
- ES6学习之路10----Symbol
- ES6中static关键字使用
- 随笔-ES6 class 封装性实现(私有属性)