Consider this simpler example:
function foo({ bar }) {
console.log(bar);
}
You can call this like foo({ bar: 42 });
and get 42
in the console.
But say you want a default parameter, you want bar
and baz
, but make baz
optional, with a default value of true
, you can do it like so:
function foo({ bar, baz = true }) {
console.log(bar, baz);
}
Call that one with foo({ bar: 42 })
would result in 42, true
.
Now say we want all of the arguments to be optional:
function foo({ bar = 42, baz = true }) {
console.log(bar, baz);
}
foo({}); // 42, true
// however
foo(); // TypeError: Cannot destructure property `bar` of 'undefined' or 'null'.
Oops, you can't destructure a value that wasn't passed. So you need that parameter to have a default too:
function foo({ bar = 42, baz = true } = {}) {
console.log(bar, baz);
}
foo(); // 42, true. Yey!
Therefore, for your specific example:
function drawES2015Chart({size = 'big', cords = {x: 0, y: 0}, radius = 25} = {}) {
...
}
accepts one optional parameter, an object with three optional keys:
size
is an optional key with a default value of big
cords
is an optional key with a default value of {x: 0, y: 0}
radius
is an optional key with a default value of 25
And because all of the keys are optional, we assume that empty input is equivalent to empty object, which would in turn use all of the default values for our keys.