# nested default values

[JS](../../../../..) ⟩ [operator](../../..) ⟩ [assignment](../..) ⟩ [destructuring](..) ⟩ nested default values

{% tabs %}
{% tab title="💈範例" %}
* replit： [nest default values](https://replit.com/@pegasusroe/nested-default-values#index.js)

```javascript
// ⭐ test cases
const opts = [
    {},
    { pad: {} },
    { pad: { length: 10 } },
    { pad: { length: 1, char: 'x' } },
];

// ⭐ destructuring arguments
function print({

    prefix = true,

    // ⭐ `pad` might be overridden❗
    pad = {
        length: 0,    // ⭐ nested default value
        char: '_',
    },

} = {}) {
    
    // ⭐ better destructure `pad` again❗
    const { length = 0, char = '_' } = pad;
    
    console.log(prefix, char, length, pad);
}

for (const opt of opts) {
    print(opt);
}

// P    C  L         pad
// -----------------------------------
// true _  0  { length: 0, char: '_' }
// true _  0  {}
// true _ 10  { length: 10 }
// true x  1  { length: 1, char: 'x' }
```
{% endtab %}

{% tab title="📗 參考" %}
* [x] JS.info ⟩ [nested destructuring](https://javascript.info/destructuring-assignment#nested-destructuring)
* [ ] Ronald Chen ⟩ [Destructuring Nested Objects](https://medium.com/@pyrolistical/destructuring-nested-objects-9dabdd01a3b8)
* [ ] Jay Cruz ⟩ [How to Destructure Nested JavaScript Objects](https://dev.to/coderjay06/how-to-destructure-nested-javascript-objects-1dcl)
{% endtab %}

{% tab title="👥 相關" %}
* [destructured](../../../../../val/func/param/destructured "mention")
* [arr](../arr "mention")
* [obj](../obj "mention")
{% endtab %}

{% tab title="⬇️ 應用" %}
* [n.tohex](../../../../../val/prim/num/number+ext/n.tohex "mention")
{% endtab %}

{% tab title="🗣 討論" %}
* [ES6 deep nested object destructuring](https://stackoverflow.com/questions/40622385/es6-deep-nested-object-destructuring)
* [Destructuring with nested objects and default values](https://stackoverflow.com/questions/39049399/destructuring-with-nested-objects-and-default-values)
{% endtab %}
{% endtabs %}
