可能重复:
Use of .apply() with 'new' operator. Is this possible?使用应用与功能构造
我有5个或形式
var analyteSelection = new TemplatedSelectionContainer($('.analyte-container', this), helpers, optionsTemplate);
var instrumentSelection = new AssetBackedSelection($('.instrument-container', this), helpers, optionsTemplate, Assets.instruments, 'Instrument');
var methodSelection = new AssetBackedSelection($('.method-container', this), helpers, optionsTemplate, Assets.methods, 'Method');
正如你所看到的6个变量赋值,一个这些构造函数的大部分非常相似。这将是很好,如果我可以创建一个小的通用钻营建设者,让我做一些事情,如:
var newSel = selectionContainerBuilder(this, helpers, optionsTemplate)
var analyteSelection = newSel(TemplatedSelectionContainer, '.analyte-container');
var instrumentSelection = newSel(AssetBackedSelection, '.instrument-container', Assets.instruments, 'Instrument');
var methodSelection = newSel(AssetBackedSelection, '.method-container', Assets.methods, 'Method');
我能实现类似的东西与
var selectionContainerBuilder = function(ctx, helpers, optionsTemplate) {
return function(FuncDef, selector, a, b, c, d, e, f) {
return new FuncDef($(selector, ctx), helpers, optionsTemplate, a,b,c,d,e,f);
}
}
但严重的ICK。我希望能够将前三个已知参数拼接到参数数组的开头,并将其应用到FuncDef,但我被使用新运算符的需要所挫败。
而之前有人问,我不能做FuncDef内的新操作员执法,因为它是由coffeescript类关键字生成的。
噢,那是......好点的实际。嗯,不知道这是否有任何范围问题? –
作用域不应该成为问题,因为'cls'和'args'在它们传递给'newApply'时将它们的作用域链与它们连接在一起。 – user123444555621
我想知道为什么(a)你选择了这个答案,而不是从另一个问题和(b)中接受的答案,你没有投票决定把它作为一个副本来结束? –