let 和 const
- 块级作用域
- let 和 const 声明的变量,只在块级作用域有效(大括号包裹的部分就是块级作用域)
{
let a = 10;
}
console.log(a) // Uncaught ReferenceError: a is not defined
let a = 10;
let a = 20; // Uncaught SyntaxError: Identifier 'a' has already been declared
解构赋值
a、数组的解构赋值
// 案例一
let a, b;
[a, b] = [1, 2]; // a = 1; b = 2
// 案例二
let a, b, rest;
[a, b, ...rest] = [1, 2, 3, 4, 5, 6]; // a = 1; b = 2; rest = [3, 4, 5, 6]
// 案例三:没有找到赋值的对象,则为 undefined
let a, b, c, rest;
[a, b, c] = [1, 2]; // a = 1; b = 2; c = undefined
b、对象的解构赋值
let {a, b} = {a: 1, b: 2, c: 3}; // a = 1; b = 2
网友评论