I have a function called "Time", it accepts from 0 to 6 arguments.
function Time(year,month,day,hour,minutes,options) {
}
Parameters month, day, hour and minutes cannot be defined if the previous one isn’t defined.
Even if the "options" parameter is optional, if present, it has to be the last parameter.
However, user can define only year and month for example, and then options.
How to avoid the user to have to write this:
var newTimeInstance = Time(2024,06, undefined, undefined, undefined, options {
fr: {
// ...
},
es: {
// ...
}
})
If it’s not possible, is there another way to join options to a function ?
I’ve tried: var newTimeInstance = Time(2024,01).options({ ... })
but then I’m unable to access the other constructor functions in newTimeInstance.
2
Answers
Inconvenient, but you could do:
Use
Array::reduceRight()
to checkarguments
: