Destructuring in Function Parameters
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Unpacking Right in the Parameters
A function that receives an object usually starts by reading a few properties from it. That's repetitive:
function describe(cdm) {
console.log(cdm.name + " is level " + cdm.level);
}
describe({ name: "Zap", type: "electric", level: 4 });You can destructure in the parameter list itself. The function then receives the properties directly, already named:
function describe({ name, level }) {
console.log(name + " is level " + level);
}
describe({ name: "Zap", type: "electric", level: 4 });Exercise 2 β The Call Doesn't Change
Even though `{ name, level }` looks like two parameters, the function still expects a single object as its argument. You call it by passing the whole object, just like before:
const zap = { name: "Zap", type: "electric", level: 4 };
function label({ name, level }) {
return name + " Lv." + level;
}
console.log(label(zap));Bonus: just by reading `function label({ name, level })`, you immediately see which properties the function uses without reading its body.
Exercise 3 β Quiz: Destructured Parameter
What does this code print?
function price({ name, cost }) {
return name + " costs " + cost;
}
console.log(price({ name: "Potion", cost: 30 }));Potion costs 30name costs costundefined costs undefinedHint : The names between the braces are matched against the object's keys.
Exercise 4 β Destructure the Parameter
Make the function receive `name` and `hp` directly.
Hint : The pattern in the parameter list looks like the object you expect to receive.
Exercise 5 β Destructuring Array Parameters
Just like with objects, you can destructure an array directly in a function's parameters using square brackets `[]`. This is great when a function receives an array whose elements have a fixed order:
function report([winner, loser]) {
console.log(winner + " defeated " + loser);
}
report(["Pyrochu", "Zap"]);Parameter destructuring also shines inside array methods like `forEach`: when looping over an array of objects, you can unpack each object's properties right in the callback parameter:
const team = [
{ name: "Aquachu", level: 12 },
{ name: "Zap", level: 4 }
];
team.forEach(({ name, level }) => {
console.log(name + " Lv." + level);
});Exercise 6 β Quiz: Destructured Callback
What does `names` contain?
const team = [
{ name: "Zap", level: 4 },
{ name: "Metachu", level: 15 }
];
const names = team.map(({ name }) => name);["Zap", "Metachu"][4, 15][undefined, undefined]Hint : The pattern replaces the callback's parameter, and pulls out one property.
Exercise 7 β Unpack in the Callback
Build an array of the levels, destructuring in the callback's parameter.
Hint : The property you pull out is also the value you return.
Exercise 8 β Parameter Patterns
Match each signature with what it expects.
f(cdm)f({ name })f([a, b])f({ name: n })Exercise 9 β Battle Announcer
Write the announcer's functions, destructuring every parameter. Not a single dot after a parameter name is allowed.
Step 1: `announce({ name, level })` returns `"name (Lv.X) enters the arena!"`. Step 2: `duel([first, second])` receives an array of two names and returns `"first vs second"`. Step 3: print the announcement for every member of the team, then print the duel between the first two names of `pair`.
Expected output:
Aquachu (Lv.12) enters the arena!
Zap (Lv.4) enters the arena!
Aquachu vs Zapconst team = [
{ name: "Aquachu", level: 12 },
{ name: "Zap", level: 4 }
];
const pair = ["Aquachu", "Zap"];
// Write your functions here
// Use them here