lundi 23 décembre 2019

Is there a way to avoid passing all the parameters to a function [duplicate]

This question already has an answer here:

Suppose i have something like this:

createSettings(a = null, b = null, c = null, d = 50) {
}

I would really like to avoid calling my functions like this:

createSettings(null, null, null, 60)

I know one way is to accept an object instead of a multiple parameters:

createSettings(settings) {
}

And then i would call it in this way:

createSettings({d: 60})  

But the problem with this approach is that i lose the advantage of knowing what i should pass to the function!, meaning the function only says pass a setting, it doesn't say the setting can accept a, b, c, d !, which is kinda important because it helps me understand how to use a function, and my functions usually are complicated and knowing what to pass is really important as well.

So here is my question, is there a way to combine these two approaches and have a function:

createSettings(a = null, b = null, c = null, d = 50) {
}

And then called it like this?:

createSettings(...{d: 50})  

Any solution or suggestion is much appreciated.

Aucun commentaire:

Enregistrer un commentaire