First of all, I would recommend sticking to one assessment. As you said, use either "named"
function foo({a = 'peanut', b = 'butter'} = {}) { console.log(a, b); }
or positional arguments:
function foo(a = 'peanut', b = 'butter') { console.log(a, b); }
Choose the one that best suits your function does not mix both .
If you really need both for some reason, standard overload methods are available to you. It will only work if your first positional argument is not an object. I would suggest one of the following idioms:
function foo(a, b) {
function foo({a, b}) {
and if you need default values, it gets ugly anyway:
function foo(a, b) { var opts = (arguments.length == 1 && typeof arguments[0] == "object") ? arguments[0] : {a, b}; ({a = 'peanut', b = 'butter'} = opts); console.log(a, b); }
Bergi source share