I'm learning about javascript and I see this block of code that I don't understand:
exports.configure = ({
expressapp = null,
userdb = null,
path = '/myroute'
} = {}) => {
// handle routes
};
I'm most confused about the structure of the argument being passed in and what is going on inside there. Where can I find more information about that so I can read about it? What is it even called when you pass arguments like that? Why would you want to do it this way?
The pattern is a destructuring assignment which assigns a plain object as the default parameter, to avoid TypeError
if no value is passed to the function.
const exports = {};
exports.configure = ({
expressapp = null,
userdb = null,
path = '/myroute'
}) => {
// handle routes
console.log(expressapp)
};
try {
exports.configure();
} catch(err) {
console.error(err)
}
const exports = {};
exports.configure = ({
expressapp = null,
userdb = null,
path = '/myroute'
} = {}) => {
// handle routes
console.log(expressapp)
};
try {
exports.configure();
} catch(err) {
console.error(err)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With