๐Ÿ”ธdefault parameter

๐Ÿšง under construction -> previous parameters

JS โŸฉ object โŸฉ function โŸฉ parameter โŸฉ default parameter

๐Ÿšง

const a = 3;     // โ—this one is NEVER referenced in h0 ~ h3 below
const obj = {a}; // { a: 3 }

// ---------------------------------------------------------------------------
// โ›” "default value" ่ˆ‡ "paremeter name" ๆฒ’่พฆๆณ•็”จใ€ŒๅŒไธ€ๅ€‹ๅๅญ—ใ€โ—
// ---------------------------------------------------------------------------
function h0({a} = {a: a}) { return a } // โ›” ReferenceError: Cannot access 'a' before initialization
function h1({a} = {a}) { return a }    // โ›” ReferenceError: Cannot access 'a' before initialization
function h2({a} = { }) { return a }    // โ— a = undefined (by default)
function h3({a}      ) { return a }    // โ— 
function h4({a=a} = {}) { return a }   // โ›” ReferenceError: Cannot access 'a' before initialization

// this one is OK.
function g ({a} = {a: 0}) { return a } // โœ…
const {log} = console;

const a = 3;     // โ—this one is NEVER referenced in h0 ~ h4 below
const obj = {a}; // { a: 3 }

// ---------------------------------------------------------------------------
// โ›” "default value" ่ˆ‡ "paremeter name" ๆฒ’่พฆๆณ•็”จใ€ŒๅŒไธ€ๅ€‹ๅๅญ—ใ€โ—
// ---------------------------------------------------------------------------
function h0({a} = {a: a}) { return a } // โ›” ReferenceError: Cannot access 'a' before initialization
function h1({a} = {a}) { return a }    // โ›” ReferenceError: Cannot access 'a' before initialization
function h2({a} = {} ) { return a }    // โ— a = undefined (by default)
function h3({a}      ) { return a }    // โ— 
function h4({a=a} = {}) { return a }   // โ›” ReferenceError: Cannot access 'a' before initialization

// this one is OK.
function g ({a} = {a: 0}) { return a } // โœ…

// โญ default value + previous parameters + object destructuring
function f1({x=1, y=x+2}={}) { return x+y }
function f2(x=1, {y=x+3}={}) { return x+y }

// โญ default value + previous parameters 
function f3(x=1, y=x+2) { return x+y }

// โญ default value
function f4(x=1, g = (t) => 2*t){ return x + g(x) }

// log
// ---------------------------------------------------------------------------
;[
    f1(),        // 4: x=1, y=3, x+y=4
    f2(),        // 5: x=1, y=4, x+y=5
    f3(),        // 4: x=1, y=3, x+y=4
    f3(2),       // 6: x=2, y=4, x+y=6
    f4(),        // 3: x=1, x + 2*x = 3
    f4(2),       // 6: x=2, x + 2*x = 6
    f4(2, (x) => x / 3),    // 2.6666666666666665: x = 2, return x + x/3

    g(),         // 0
    h2(),        // undefinedโ—
    h3(1),       // undefinedโ—
    
].forEach(x => log(x));

// โ— log expressios that throw
// ---------------------------------------------------------------------------
[
    `f1({y: x + 5})`,  // โ›” [ReferenceError] x is not defined
    `f2({y: x + 5})`,  // โ›” [ReferenceError] x is not defined

    `h0()`,            // โ›” [ReferenceError] Cannot access 'a' before initialization
    `h1()`,            // โ›” [ReferenceError] Cannot access 'a' before initialization
    `h3()`,            // โ›” [TypeError] Cannot destructure property 'a' of 'undefined'
    `h4()`,            // โ›” [ReferenceError] Cannot access 'a' before initialization
    
].forEach(exprStr => {
    try { log(eval(exprStr)) } 
    catch (e) { log(`โ›” [${e.constructor.name}]`, e.message); }
});

Last updated