Optional named arguments
Someone else should feel free to correct me, but I think that explicit optional named arguments have been eaten by options objects + argument destructuring.
<burp>
nothing left...
</burp>
Thanks for your answers.
Domenic Denicola sent me an example by email, but I don't understand it all.
I waited for his response, but seemed busy, I turn back to you.
The sample :
function postToMailingList({
email = '[email protected]',
subject = 'Optional Named Arguments',
from = '[email protected]'} = {}) {
console.log(email, subject, from)
}
postToMailingList() //uses all the defaults
postToMailingList({})//exactly the same
postToMailingList({email: '[email protected]'})//overrides the default e-mail
Several points seems obscure:
- the
=
operator in the option object declaration - the role of
= {}
inside the parentheses of the function declaration - and the fact that you can decide to have an empty object, despite defaults (line 9)
Could you enlighten me, please? or advise me of an article on this subject?
Thanks in advance.
The =
operator in the object declaration is defining defaults for the destructuring. If the object being destructured doesn't have that property, the value on the right hand side of the =
operator is used.
The = {}
provides a default object to de-structure, making the entire argument optional. Without that you would be trying to de-structure undefined
when you call postToMailingList()
which would be an error.
If you don't pass an object, {}
is used because it is the default. If you pass an empty object then that empty object is used (i.e. still {}
).
Hello everyone,
What's the point about optional named arguments support, please?
ES6? ES7? abandoned?