Reputation: 3518
It's now possible to destructure function parameters like so:
function add({a, b}) { return a + b; }
Which can be called like this:
add({a: 5, b: 9});
Is it also possible to combine that with positional arguments so it can also be called without naming the arguments. E.g.:
add(5, 9);
Upvotes: 0
Views: 50
Reputation: 191976
You can use the rest parameter, and destructure it according to it's length.
function add(...args) {
let a, b;
args.length === 1 ? ({ a, b } = args[0]) : [a, b] = args;
return a + b;
};
console.log(add({a: 5, b: 9}));
console.log(add(5, 9));
Upvotes: 3