node.js如何批量赋值

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

猜你喜欢

转载自www.cnblogs.com/chenqionghe/p/11411544.html
今日推荐