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

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

问题描述

Coffeescript使用存在运算符来确定变量何时存在,并且在 coffeescript文档中,它显示 something?会编译为 something!== undefined&&但是我注意到我的coffeescript版本只编译这个到 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!"



which compiled to

// 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?

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,除非变量为 null strong> undefined ,这类似于Ruby的 nil?

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

所以当然会说没有taco!:

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

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

您的 taco C> undefined 因此 taco?为false。

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&&& taco!== null 。你仍然看到是 null 检查(更紧凑的形式),但也有有一个 var taco 请使用 typeof ;注意, typeof taco 测试也检查 taco = undefined ,因此更严格的!== test可用于查看 taco 是否为 null

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.

你说这个:


我注意到我的coffeescript版本只是编译为 something!== null

但这不是它在做什么,到 something!= null ;注意使用sloppy类型转换不等式(!= )与严格不等式(!== )你说是有。 != 之间的区别在这里很重要

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 and Undefined Types are == (but not ===)

所以如果你知道变量 v 已经被声明(即有 var v 某处),则 v!= null 足以检查 v 既不是 null 也不是 undefined 。但是,如果你不知道 v 已经被声明,那么你需要一个 typeof 检查,以避免引用错误您尝试将未声明的变量与 null 进行比较。考虑这个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!");
}

这会抛出一个ReferenceError,因为 taco 不存在。这:

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!");
}



检查是否尝试访问尚未声明的内容。我不认为你可以构建第一个在CoffeeScript没有嵌入JavaScript使用反引号。

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天全站免登陆