let 和 const、解构赋值

2018-05-06  本文已影响10人  落叶无声xcm

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
上一篇下一篇

猜你喜欢

热点阅读