【发布时间】:2015-08-10 04:15:10
【问题描述】:
前言
这可能没有真正的实现,但在阅读了another post on coffeescript's 2 switch usages 之后,我决定尝试一下,发现这个相当奇怪的问题。
咖啡脚本
type = 'guidance'
s = switch
when type is 'guidance'
'g'
when type is 'vulnerability'
'v'
else
'foo'
console.log s #g
转译的 javascript
var s, type;
type = 'guidance';
s = (function() {
switch (false) {
case type !== 'guidance':
return 'g';
case type !== 'vulnerability':
return 'v';
default:
return 'foo';
}
})();
console.log(s); //g
难题
我没有得到的是 case 表达式被编译为相反的值。 Coffeescript when type is 'guidance' 应该 转换为以下 javascript case type === 'guidance' 对吗?
如果您使用 s = switch true,则 case 表达式将正确呈现 case type === 'guidance'。不管渲染的 javaScript case 表达式如何,结果都是一样的。
【问题讨论】:
-
请重新阅读您的问题。代码转换为您说它应该转换为的确切内容。
-
这是复制粘贴的错字。已更正
-
我不会说这是一个“难题”。转译结果有效,这正是 coffeescript 决定实现它的方式。
标签: javascript coffeescript switch-statement theory