How can I have a function accept either named arguments (foo({a: 'hello', b: 'it is me'})
) or positional arguments (foo('hello', 'it is me')
)?
I understand that named arguments can be simulated by passing an object to the function:
function foo(options) {
options = options || {};
var a = options.a || 'peanut'; // whatever default value
var b = options.b || 'butter'; // whatever default value
console.log(a, b);
}
// ES6 allows automatic destructuring
function foo({a = 'peanut', b = 'butter'} = {}) {
console.log(a, b);
}
But that does not allow me to accept positional arguments to be passed.
I would like to use ES6 but anything from ES5 would be ok too.