1. 數組解析賦值
let a = 1;
let b = 2;
let c = 3;
等同於
let [a, b, c] = [1, 2, 3];
默認值
let [a, b = "B"] = ["a", undefined]
console.log(a, b)
當賦值為undefined的時候,默認值會生效
2.對象解析賦值
let { foo, bar } = { foo: 'A', bar: 'B' };
console.log(foo ,bar)
//A B
默認值
let {x, y = 5} = {x: 1};
console.log(x, y)
//1 5
3. 字符串解析賦值
const [a, b, c, d, e] = 'hello';
4. 函數參數解析賦值
function add([x, y]){
return x + y;
}
console.log(add([1, 2])); // 3