【发布时间】:2011-12-01 19:15:39
【问题描述】:
我有 5 或 6 个形式的变量赋值
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');
如您所见,这些构造函数中有很大一部分非常相似。如果我可以创建一个通用的柯里化构建器来允许我执行以下操作,那就太好了:
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);
}
}
但是真的很恶心。我希望能够将前三个已知参数拼接到参数数组的开头并将其应用于 FuncDef,但我因需要使用 new 运算符而受阻。
在有人问之前,我不能在 FuncDef 中执行 new-operator 强制,因为它是由 coffeescript 类关键字生成的。
【问题讨论】:
标签: javascript