【发布时间】:2024-01-19 09:49:01
【问题描述】:
这是我的功能
st3 = (x, y) ->
console.log "#{x?}, #{y?}"
if [x?, y?] is [true, true] # <- this is the line
'good'
'bad'
这是输出
true, true
bad
我希望能够像在 python 中那样进行元组比较。
在python中,if可以大致写成
if (x, y) == (True, False):
return 'good'
coffescript if 循环被翻译成这样的javascript
if ([x != null, y != null] === [true, true]) {
'good';
}
这就是为什么 this 不会被评估为 true。
在coffeescript中有没有其他的表达方式?
【问题讨论】:
-
你不能用
if x is true and y is true吗? -
在这种情况下是肯定的。但是如果元组大小增加了怎么办?我正在寻找更优雅的解决方案。
-
这里没有什么不优雅的,如果你有任意大小的参数,你可以循环参数并评估条件。或者,您可以编写自己的元组原型并从那里扩展。
标签: coffeescript