首页 > CoffeeScript 与存在相关操作符

CoffeeScript 与存在相关操作符

CoffeeScripte 中 existential operator 在编译成 javascript 时

 //coffee
console.log x?

//js
console.log(typeof x !== "undefined" && x !== null);

为什么不是

console.log(x !== undefined && x !== null);

为什么在判断 undefined 的时候,不直接判断呢? 这样做的好处是什么


因为undefined并不是保留字,可以被覆盖,如:

undefined = 1;

也是可以的。这样一来直接判等就无效了

【热门文章】
【热门文章】