0
constructor(
      public templateHeaderRow = 'DefaultTableHeaderTemplate',
      public templateBodyRow = 'DefaultTableRowTemplate',
      private templateBase = _templateBase) {

          require([this.templateBase + templateHeaderRow + '.html', this.templateBase+ templateBodyRow + '.html'], () => console.log('fdsfdsdfsf'));
}

Then I am calling it like this: log = new ListViewBase(undefined,'LoggingTableRowTemplate');

It seems a little stupid with the (undefined,'. Can anyone who can suggest a different design?

What would be nice, would be something like in C# where I can do (parametername:value, another:value2), and the order doesn't matter for the optional parameters. Haven't found anything like that in typescript though.

Update

Alternative I am doing this:

public templateHeaderRow = 'DefaultTableHeaderTemplate';
public templateBodyRow = 'DefaultTableRowTemplate';
private templateBase = _templateBase;

constructor(configuration? : ListViewBaseConfiguration) {
    if (typeof configuration !== "undefined") {
        if (typeof configuration.templateBase !== "undefined")
            this.templateBase = configuration.templateBase;
        if (typeof configuration.templateBodyRow !== "undefined")
            this.templateBodyRow = configuration.templateBodyRow;
        if (typeof configuration.templateHeaderRow !== "undefined")
            this.templateHeaderRow = configuration.templateHeaderRow;
    }


    require(['template!' + this.templateBase + this.templateHeaderRow + '.html',
        'template!' + this.templateBase + this.templateBodyRow + '.html']);
}

But I have to write alot more code to get the behavior that some parameters can be set and others not.

1 Answer 1

2

I can't really add to what you already know here.

If you use default values on your parameters, your best bet is to order them by likelihood, i.e. the most likely to be passed goes first.

Passing an object could be made slightly less onerous if you got defaults first, for example:

var config = MyClass.GetConfig(); // static method call
config.templateBodyRow = 'Not Default';

You could then reasonably expect all values in the object passed to the constructor.

Another option might be JavaScript's equivalent of null-coalesce:

this.templateBodyRow = config.templateBodyRow || 'DefaultBodyRowTemplate';

It is just shorter than your version.

This is just one option - in the absence of named arguments, you are choosing the best of an inadequate bunch!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.