1.5 KiB
1.5 KiB
title |
---|
Use Destructuring Assignment to Pass an Object as a Function's Parameters |
Use Destructuring Assignment to Pass an Object as a Function's Parameters
You could pass the entire object, and then pick the specific attributes you want by using the .
operator. But ES6 offers a more elegant option!
Hint 1:
Get rid of the stats
, and see if you can destructure it. We need the max
and min
of stats
.
Spoiler Warning - Solutions Ahead!
Solution 1:
const half = (function() {
"use strict"; // do not change this line
// change code below this line
return function half({max, min}) {
// use function argument destructuring
return (max + min) / 2.0;
};
// change code above this line
})();
Notice that we are destructuring stats
to pass two of its attributes - max
and min
- to the function. Don't forget to the modify the second return statement. Change stats.max
to just max
, and change stats.min
to just min
.
Solution 2:
Here is another solution that works. Not much of a difference, other than the fact that the function doesn't have a name.
const half = (function() {
"use strict"; // do not change this line
// change code below this line
return (({max, min}) => {
// use function argument destructuring
return (max + min) / 2.0;
});
// change code above this line
})();