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

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

それから私はそれを次のように呼んでいます:log = new ListViewBase(undefined,'LoggingTableRowTemplate');

で少しばかげているよう(undefined,'です。別のデザインを提案できる人はいますか?

私ができるC#のようなもので(parametername:value, another:value2)、オプションのパラメータの順序は関係ありません。ただし、タイプスクリプトでそのようなものは見つかりませんでした。

アップデート

代替私はこれをやっています:

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']);
}

しかし、一部のパラメーターを設定でき、他のパラメーターを設定できないという動作を得るには、さらに多くのコードを記述する必要があります。

4

1 に答える 1

2

あなたがここですでに知っていることを私は本当に追加することはできません.

パラメータにデフォルト値を使用する場合、最善の方法は、可能性の高い順に並べることです。つまり、渡される可能性が最も高いものが最初になります。

最初にデフォルトを取得すると、オブジェクトを渡すことの煩わしさを少し軽減できます。次に例を示します。

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

そうすれば、コンストラクターに渡されるオブジェクトのすべての値を合理的に期待できます。

もう 1 つのオプションは、JavaScript の null-coalesce に相当するものです。

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

あなたのバージョンよりも短いだけです。

これは 1 つのオプションにすぎません。名前付き引数がない場合は、不適切な束の中から最適なものを選択していることになります。

于 2013-09-14T18:13:21.140 に答える