引数配列を使用して新しいViewインスタンスを作成する必要があります。電話したくないので
new View(array)
私はこの解決策を試しました。ただし、残念ながら、これは機能しません。つまり、初期化関数に引数が渡されません。では、配列を渡す新しいビューを作成する方法はありますが、初期化関数で引数を1回だけ持つのでしょうか。
引数配列を使用して新しいViewインスタンスを作成する必要があります。電話したくないので
new View(array)
私はこの解決策を試しました。ただし、残念ながら、これは機能しません。つまり、初期化関数に引数が渡されません。では、配列を渡す新しいビューを作成する方法はありますが、初期化関数で引数を1回だけ持つのでしょうか。
これは、ちょっとしたプロトタイプの策略で実現できます。
function createView() {
var args = arguments;
//create a factory function, so we can get the "this" context
function Factory() { return YourViewClass.apply(this, args);}
//assign factory prototype (essentially makes factory a YourViewClass)
Factory.prototype = YourViewClass.prototype;
//invoke factory function
return new Factory();
};
var view1 = createView({model:model}, 1, 2, 3);
var view2 = createView.apply(null, argumentArray);
可変引数を使用して任意の「クラス」(コンストラクター関数) をインスタンス化するための一般的な解決策:
function instantiate(ctor) {
//strip first arg, pass rest as arguments to constructor
var args = Array.prototype.slice.call(arguments, 1);
function Factory() { return ctor.apply(this, args);}
Factory.prototype = ctor.prototype;
return new Factory();
};
//call directly
var view1 = instantiate(YourViewClass, {model:model}, 1, 2, 3);
//partially apply to create a factory for a specific view class
var viewFactory = _.partial(instantiate, YourViewClass);
var view2 = viewFactory({model:model}, 1, 2, 3);