samanime samanime - 21 days ago 5
Javascript Question

Get object that was destructured in parameter

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.

Answer

You were sooo close. Use an equals sign instead of a colon to name the parameter.

const myFunc = (allArgs = {a, b, c}) => {
    console.log(allArgs);
};
myFunc({a:1, b:2, c:3}); // Output: {"a":1, "b":2, "c": 3}

Comments