ES6新增特性 (一
1. let关键字
1.1 无法重复声明变量
let a = 1;
let a = 2; // Uncaught SyntaxError: Identifier 'a' has already been declared
1.2 引进块级作用域
{
let a = 1;
}
console.log(a) // Uncaught ReferenceError: a is not defined
1.3 不存在变量提升
console.log(a); // Uncaught ReferenceError: Cannot access 'a' before initialization
let a = 1;
2. const关键字
2.1 变量名一般使用全大写
const SCHOOL = 'YC';
2.2 需赋初值
const SCHOOL; // Uncaught SyntaxError: Missing initializer in const declaration
2.3 当值是基本数据类型时,不可更改
const SCHLLO = 'YC';
SCHLLO = 'ZY'; // Uncaught TypeError: Assignment to constant variable
2.4 具有块级作用域
{
const SCHLLO = 'YC';
}
console.log(SCHLLO); // Uncaught ReferenceError: SCHLLO is not defined