I've been using babel on a few projects, and on my last project I got myself with this question;
I have a scan function where I want to set 2 default values
export default function scan({
tmpFolder = "./tmp",
verbose = false
}){
// function body
}
When importing the module into my bin project I have to set an empty {}
as param
var scan = require('scan');
scan({})
Otherwise, if I dont set it;
var scan = require('scan');
scan()
i'm getting an error;
var _ref$tmpFolder = _ref.tmpFolder;
^
TypeError: Cannot read property 'tmpFolder' of undefined
Why am I getting an error if I don't set an empty {}
? Shouldnt be {tmpFolder = "./tmp",verbose = false}
the default value ? Is this a Babel issue or is it wrong to don't set an empty {}
in this case ?
Your logic splits up into
which should make it clear that when you call the function with no arguments, you are assigning
undefined
to an object pattern, which makes it throw. To work, you need to giveparam
a default value, e.g.or combine all together: