javascript - 关于 Coffeescript/Javascript 条件结果的问题

标签 javascript coffeescript

我有这个 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) return false when index is 1? (index > 0) returns true 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 when index is 0?

何时 I tried it myself测试通过。此行为可能是由您的测试代码中未包含在帖子中的某些内容引起的。

关于javascript - 关于 Coffeescript/Javascript 条件结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221046/

相关文章:

javascript - if 语句中的速记 promise 未按预期工作

javascript - 在 Jasmine 中模拟假按键

javascript - 需要帮助实现 jquery 指令的排序

javascript - 带有自定义 json 配置的 grunt 任务

javascript - 将 Coffeescript 中的 true 和 false 分别转换为 1 和 -1

javascript - 使用 CoffeeScript 将方法传递到 SetTimeOut

javascript - 更改 DIVS 数组的样式

javascript - 保存 XML 文件、JS、XML HTML

javascript - 将 HTML 表单输入到表格中

ajax - 脊柱.js: "Uncaught Unknown record"