所以我正在玩咖啡脚本并得到这种行为:
a?将转化为typeof a !== "undefined" && a !== null;- 那没关系
问题是如果我尝试相同的a.foo?.这次它转化为a.foo != null;
我得到的相同结果a[foo].
是否有"快速"的方法来检查是否a.foo为null或未定义,如有?
Coffeescript使用存在运算符来确定变量何时存在,并且在coffeescript文档中它显示something?会编译到something !== undefined && something !== null但是我注意到我的coffeescript版本只是编译它something !== null所以我写了一个测试来看看这将如何影响我的代码
taco = undefined
if taco?
console.log "fiesta!"
else
console.log "No taco!"
Run Code Online (Sandbox Code Playgroud)
编译为
// 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);
Run Code Online (Sandbox Code Playgroud)
并输出了一些意外,No taco!所以我的问题是两倍.为什么coffeescript不再检查价值是什么undefined以及为什么这是有效的?