关于Coffeescript/Javascript条件结果的问题
Questions about Coffeescript/Javascript conditional results
我有这个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
测试通过?
为什么
index
为1,(index is not 0)
返回false
?对于1,(index > 0)
返回true
,那么为什么(index is not 0)
不返回呢?
CoffeeScript不使用is not
的不等式,它使用!=
和isnt
。通过查看编译后的代码,我们可以看到它实际上将(index is not 0)
解释为(index is (not 0))
。
当我自己尝试时,测试没有通过。此行为可能是由您的测试代码中没有包含的某些内容引起的。为什么
index
为0时unless index is 0
测试通过?
这很繁琐:
console.log(index === !0);
它的处理方式与
相同console.log(index === (!0));
0
是一个假常数,所以你可以用true
代替(!0)
。真正的代码是:
console.log(index === true);
因此,只有当index
为布尔值为true时,它才会记录"true",而不需要类型强制转换。
相关文章:
- async问题,JS Promise无法返回结果,但可以使用console.log
- 获取json结果的身份验证问题
- 使用 jQuery UI 折叠面板显示 couchDB 数据库搜索结果时出现问题
- 在Javascript/Google Maps Api中将结果放入数组时出现问题
- node.js+MySQL;JSON结果-回调问题&没有响应客户端
- NaN作为函数结果而不是必需的整数出现问题
- 显示更多结果的问题
- 管道订购问题?将stdin管道化为http请求,并将结果管道化为stdout
- I'我在使用聚合核心ajax发送表单结果时遇到问题-go没有执行
- 发送 XMLHttpRequest 时缓存结果的问题
- J查询结果显示问题
- JS正则表达式结果表达式赋值问题
- AngularJS ng-repeat - 空白结果的问题
- 简单搜索结果页面:if/else 使用 localStorage 变量和 document.write 时出现问题
- 带有json结果列问题的Highchart瀑布
- 使用Meteor Search Source软件包获取结果的反应性问题
- 表单显示问题+数组下的结果
- Javascript数学给出了不正确的结果,我该如何解决这个问题
- .toFixed() 结果的问题
- 通过jquery将多个结果发送到php页面中的问题