Suppose I have a function that takes a destructured object as a parameter in an arrow function:
const myFunc = ({a, b, c}) => {
};
Is there anyway or syntax that would allow me to get that whole object as a single value as well? Since the arrow function doesn't bind arguments
, I can't use that.
Is it possible to name it, something along the lines of:
const myFunc = (allArgs: {a, b, c}) => {
console.log(allArgs);
};
myFunc({a:1, b:2, c:3}); // Output: {a:0, b:1, c: 2}
Obviously, this isn't a deal breaker and there are plenty of workarounds (either don't use an arrow function, don't destructure, or recreate the object when I need it), but I'd like to know for convenience sake.
You could destructure a single param quite easily. You lose the advantage of the terse destructuring syntax in the arguments, but it's easy and still clear:
const myFunc = (allArgs) => {
const {a, b, c} = allArgs;
console.log(allArgs);
};
Alternatively, consider whether you need an arrow function. If you don't need access to the lexical this
perhaps a good old-fashioned function() {}
will do the trick and give you access to arguments
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With