How to handle nested default parameters with object destructuring?

前端 未结 3 1794
误落风尘
误落风尘 2021-01-04 13:47

I am trying to figure out if it is possible to handle multiple levels of default parameters with destructuring. Since it is not easy to explain with words, here is a step-by

3条回答
  •  春和景丽
    2021-01-04 14:22

    what about

    function fn3({foo = 'Foo', bar={} } = {}) {
       const {quux = 'Quux', corge = 'Corge'} = bar;
        console.log(foo, quux, corge);
    }
    
    
    fn3(); // Foo Quux Corge
    fn3({foo: 'Quux'}); // Quux Quux Corge
    fn3({bar: {quux: 'Baz'}}); // Foo Baz Corge
    fn3({foo: 'Quux', bar: {corge: 'Baz'}}); // Quux Quux Baz

提交回复
热议问题