thể trùng lặp:
Use of .apply() with 'new' operator. Is this possible?Sử dụng áp dụng với một constructor function
Tôi có 5 hoặc 6 bài tập biến dạng
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');
Như bạn có thể thấy, một số lượng đáng kể phần của các nhà xây dựng này rất giống nhau. Nó sẽ được tốt đẹp nếu tôi có thể tạo ra một chút builder currying generic mà sẽ cho phép tôi làm điều gì đó như:
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');
tôi có thể đạt được một cái gì đó tương tự với
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);
}
}
Nhưng nghiêm túc ick. Tôi muốn chỉ có thể ghép ba thông số đầu tiên được biết đến vào đầu của mảng đối số và áp dụng nó cho FuncDef nhưng tôi đang bị hư hỏng bởi sự cần thiết phải sử dụng toán tử mới.
Và trước khi người nào đó hỏi, tôi không thể thực hiện thao tác điều hành mới bên trong FuncDef vì nó được tạo bởi từ khóa lớp coffeescript.
Oh đó là ... một điểm tốt thực sự. Hmm, tự hỏi nếu điều này có bất kỳ vấn đề scoping? –
Phạm vi không phải là một vấn đề, vì 'cls' và' args' lấy các chuỗi phạm vi của chúng với chúng khi chúng được truyền cho 'newApply'. – user123444555621
Tôi tự hỏi tại sao (a) bạn chọn câu trả lời này và không chấp nhận câu trả lời từ câu hỏi khác và (b), bạn không bỏ phiếu để đóng bản sao này như một bản sao? –