CoffeeScript 的存在运算符是如何工作的? [英] How does CoffeeScript's existential operator work?

查看:15
本文介绍了CoffeeScript 的存在运算符是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Coffeescript 使用存在运算符来确定变量何时存在,并且在 coffeescript 文档中表明 something? 会编译成 something !== undefined &&something !== null 但是我注意到我的咖啡脚本版本只是将它编译为 something !== null 所以我写了一个测试看看这会如何影响我的代码

Coffeescript uses the existential operator to determine when a variable exists, and in the coffeescript documentation it shows that something? would compile to something !== undefined && something !== null however I noticed that my version of coffeescript was only compiling this to something !== null so I wrote a test to see how this would effect my code

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 的值,为什么这就足够了?

and outputted the somewhat unexpected No taco! so my question is two fold. Why does coffeescript no longer check for the value being undefined and why is this suficiant?

推荐答案

文档是这么说的 ?:

CoffeeScript 的存在运算符 ? 返回 true,除非变量为 nullundefined,这类似于 Ruby 的 nil?

CoffeeScript's existential operator ? returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil?

所以这当然会说没有炸玉米饼!":

so of course this will say "No taco!":

taco = undefined 
if taco?
    console.log "fiesta!"
else 
    console.log "No taco!"

你的 taco 是明确的 undefined 所以 taco? 是假的.

Your taco is explicitly undefined so taco? is false.

CoffeeScript 隐式声明变量,因此 ? 的 JavaScript 形式是依赖于上下文的.例如,如果你只是这样说:

CoffeeScript implicitly declares variables so the JavaScript form of ? is context dependent. For example, if you just say only this:

if taco?
    console.log "fiesta!"
else 
    console.log "No taco!"

你会看到 taco? 变成 typeof taco !== "undefined" &&炸玉米饼!== null.您仍然会看到它是 null"检查(以更严格的形式),但还有是否有 var taco"检查与 typeof;请注意,typeof taco 测试还会检查 taco = undefined,因此可以使用更严格的 !== 测试来查看 taconull.

you'll see that taco? becomes typeof taco !== "undefined" && taco !== null. You still see the "is it null" check (in a tighter form) but there's also the "is there a var taco" check with typeof; note that the typeof taco test also checks for taco = undefined so a stricter !== test can be used to see if taco is null.

你这样说:

我注意到我的咖啡脚本版本只是将其编译为 something !== null

I noticed that my version of coffeescript was only compiling this to something !== null

但这不是它正在做的事情,它实际上是在编译为 something != null;请注意使用草率"类型转换不等式 (!=) 与您声称存在的严格不等式 (!==).!=!== 之间的区别在这里很重要 因为:

but that's not what it is doing, it is actually compiling to something != null; note the use of "sloppy" type converting inequality (!=) versus the strict inequality (!==) that you claim is there. The difference between != and !== is important here since:

  • Null 和 Undefined 类型是 ==(但不是 ===)
  • Null and Undefined Types are == (but not ===)

因此,如果您知道变量 v 已被声明(即在某处存在 var v),那么 v != null 就足以检查 v 既不是 null 也不是 undefined.但是,如果您不知道 v 已被声明,那么当您尝试将未声明的变量与 null 进行比较时,您需要进行 typeof 检查以避免出现 ReferenceError.考虑一下这个 JavaScript:

So if you know that variable v has been declared (i.e. there is var v somewhere) then v != null is sufficient to check that v is neither null nor undefined. However, if you do not know that v has been declared, then you need a typeof check to avoid a ReferenceError when you try to compare an undeclared variable with null. Consider this JavaScript:

if(taco != null) {
    console.log("fiesta!");
} else {
    console.log("No taco!");
}

由于 taco 不存在,这将在您的脸上抛出一个 ReferenceError.这个:

That will throw a ReferenceError in your face since taco does not exist. This:

if(typeof taco !== "undefined" && taco !== null)
    console.log("fiesta!");
} else {
    console.log("No taco!");
}

另一方面很好,因为 typeof 检查可以防止尝试访问尚未声明的内容.如果不使用反引号嵌入 JavaScript,我认为您无法在 CoffeeScript 中构造第一个.

on the other hand is fine since the typeof check guards against trying to access something that hasn't been declared. I don't think you can construct the first one in CoffeeScript without embedding JavaScript using backticks.

这篇关于CoffeeScript 的存在运算符是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆