我有这个 CoffeeScript :
console.log 'TEST'
console.log index
console.log (index is not 0)
console.log (index > 0)
unless index is 0
console.log "passed test"
这是编译后的 Javascript:
console.log('TEST');
console.log(index);
console.log(index === !0);
console.log(index > 0);
_results.push(index !== 0 ? console.log("passed test") : void 0);
这是控制台输出
TEST
0
false
false
passed test
TEST
1
false
true
passed test
问题1)为什么当index
为1时,(index is not 0)
返回false
? (index > 0)
对于 1 返回 true
,那么为什么 (index is not 0)
不返回呢?
问题2)为什么当index
为0时unless index is 0
测试会通过?
最佳答案
Why does
(index is not 0)
returnfalse
whenindex
is 1?(index > 0)
returnstrue
for 1, so why doesn't(index is not 0)
?
CoffeeScript 不使用 is not
来表示不等式,它使用 !=
和 isnt
。通过查看编译后的代码,我们可以看到它实际上将 (index is not 0)
解释为 (index is (not 0))
。
Why does the
unless index is 0
test get passed whenindex
is 0?
何时 I tried it myself测试未通过。此行为可能是由您的测试代码中未包含在帖子中的某些内容引起的。
关于javascript - 关于 Coffeescript/Javascript 条件结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221046/