Coffeescript使用存在操作符来确定何时存在一个变量,并在coffeescript documentation它显示something?
将编译为something !== undefined && something !== null
但是我注意到我的版本的coffeescript只编译something !== null
,所以我写了一个测试,看看这将如何影响我的代码CoffeeScript的存在操作符是如何工作的?
taco = undefined
if taco?
console.log "fiesta!"
else
console.log "No taco!"
其编译成
// Generated by CoffeeScript 1.4.0
(function() {
var taco;
taco = void 0;
if (taco != null) {
console.log("fiesta!");
} else {
console.log("No taco!");
}
}).call(this);
和输出的有些出人意料No taco!
所以我的问题是双重的。为什么coffeescript不再检查值为undefined
,为什么这是suficiant?
不是一个确切的问题重复,但答案是一样的:http://stackoverflow.com/questions/9990117/existential-operator-and-object-properties:D – epidemian