mu is too short's answer लिए एक छोटा सा जोड़ा जा रहा है, तो आप एक switch
अभिव्यक्ति में अपनी दूसरी कोड का टुकड़ा बदल सकता है:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
switch
when status <= 0 then 'Something...'
when status <= 20 then 'Active'
when status <= 40 then 'Moderately Active'
when status <= 60 then 'Very Active'
when status <= 100 then 'Hyper Active'
else 'Something else'
यह मूलतः जावास्क्रिप्ट में एक switch (true)
कर (हालांकि सीएस संकलक एक switch (false)
statement with the negated conditions को उत्पन्न होगा के बराबर है अभिव्यक्तियों से बूलियन परिणाम सुनिश्चित करें ... मुझे लगता है)।
और कारण है कि पर्वतमाला काम नहीं करता है से अधिक switch
पर्वतमाला कि सीएस में शाब्दिक सादे पुराने जे एस सरणियों प्रतिनिधित्व करते हैं (हालांकि संकलक जब for i in [1..something]
की तरह कुछ कर रही कुछ अनुकूलन चाल करना होगा), इसलिए जब वे कर रहे हैं है एक switch
वे इलाज कर रहे हैं के अंदर पाए सिर्फ like normal array values:
// Generated JS for question's CS code:
switch (parseInt(blog.status)) {
case [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]:
status = "active";
break;
case [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]:
status = "Moderately Active";
break;
case [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]:
status = "Very Active";
break;
case (function() {
_results = [];
for (_i = 60; _i <= 100; _i++){ _results.push(_i); }
return _results;
}).apply(this):
status = "Hyper Active";
}
switch
बयान के अंदर मूल्य मूल रूप से जो केवल सरणियों के लिए पुरातन के लिए काम करता है, न कि प्रत्येक case
मूल्य ===
का उपयोग कर, की तुलना में है (और यदि यह सरणी के लिए भी काम करता है, तो यह सरणी समानता का परीक्षण करेगा, न कि switch
एड मान case
एड सरणी में निहित है)।
यह क्यों काम करेगा? यह [github मुद्दा] (https://github.com/jashkenas/coffee-script/issues/1383) ऐसा लगता है कि यह वाक्यविन्यास स्वीकार नहीं किया गया था। –