Can someone help me understand the following code?
const doit = ({
left: Left,
right: Right,
leftWeight = 1,
rightWeight = 1
}) => {
console.log(Left, Right, leftWeight, rightWeight);
};
doit({ left: '111', leftWeight: 2});
It seems to be two ways to pass default parameters, and destructuring assignment is involved somehow but how can I break this down to understand it?
Ok, it seems as though this is not an issue of default parameters and arrow functions but simply object destructuring:
const { left: Left, right: Right, leftWeight = 1, rightWeight = 1 } = { left: "111", leftWeight: 2 };
console.log(Left, Right, leftWeight, rightWeight);
This code gives the same result:
const { Left, Right, leftWeight = 1, rightWeight = 1 } = { Left: "111", leftWeight: 2 };
console.log(Left, Right, leftWeight, rightWeight);
Object destructuring apparently has the option to rename the variables: