Avoid passing undefined for optional parameters

Viewed 1673

If I have a constructor that has n optional parameters and I want to pass a value for only the last optional parameter, I have to pass undefined n-1 times.

For example:

class House() {
    constructor(door?, roof?, windows?) { }
}

If I want to instantiate a new House that doesn't have a door or a roof but does have windows, I have to pass undefined twice to the constructor:

let myHouse = new House(undefined, undefined, new Windows());

C# has named parameters, which would be ideal here.

How can I avoid passing undefined n-1 times in this scenario?

2 Answers
Related