Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get object that was destructured in parameter [duplicate]

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.

like image 539
samanime Avatar asked Nov 13 '16 02:11

samanime


1 Answers

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.

like image 141
JVDL Avatar answered Nov 03 '22 01:11

JVDL