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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM