संभव डुप्लिकेट:
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 चर कार्य, एक इन कन्स्ट्रक्टरों का महत्वपूर्ण हिस्सा बहुत समान है। यह अच्छा होगा अगर मैं एक छोटे से सामान्य currying बिल्डर कि मेरे जैसे कुछ करने के लिए अनुमति होगी बना सकते हैं:
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 के अंदर नया ऑपरेटर प्रवर्तन नहीं कर सकता क्योंकि यह कॉफ़ीस्क्रिप्ट क्लास कीवर्ड द्वारा उत्पन्न किया जा रहा है।
ओह कि है ... एक अच्छा बिंदु वास्तव में। हम्म, आश्चर्य है कि क्या इसमें कोई स्कोपिंग समस्या है? –
स्कोपिंग एक समस्या नहीं होनी चाहिए, क्योंकि 'सीएलएस' और 'तर्क' उनके साथ अपनी दायरे की श्रृंखला लेते हैं क्योंकि उन्हें 'नया ऐपली' पास किया जाता है। – user123444555621
मुझे आश्चर्य है कि क्यों (ए) आप इस उत्तर को चुनते हैं और स्वीकार किए गए किसी अन्य प्रश्न से नहीं (और), आपने इसे डुप्लिकेट के रूप में बंद करने के लिए वोट नहीं दिया? –