// ⭐ 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' }