ES6 Default Parameters in nested objects

ecmascript-6, javascript

Solution

When destructuring is mixed with default parameters, I admit the code is hard to read and write (especially when there are nested objects...).

But I think you are trying to do that:

function f({callback: {name = "cbFunction", params = "123"} = {}} = {}) {
  console.log(name);
  console.log(params);
}

f();
f({callback: {params: '789'}});

Problem

I want to have a function with default parameters inside nested objects, and I want to be able to call it either `f()` or specifying only individual parameters. ``` // A function with nested objects with default parameters: function f({ a = 1, callback = ({ name, param } = { name: "qwe", param: 123 }) } = {}) { console.log("a:", a); console.log("callback:", callback); } // And I want to run it like this: f(); f({ callback: { params: "456" } }); // But 'callback.name' becomes undefined. ```

Original source

Related problems