Monday 15 March 2010

How does CoffeeScript's existential operator work? -


Coffeescript uses the survival operator to determine if a variable exists, and it shows that Some something will be compiled! == Undefined & amp; Amp; some! == Zero However I have noticed that my version of coffeescript only something! == Zero was compiling in , so I wrote a test to see how this would affect my code.

  Taco = Taco if undefined? Console.log "Fiesta!" Else console.log "No Taco!"   

compiled

  // generated by CoffeeScript 1.4.0 (function () {var taco; taco = zero =; (if Taco! =) Null) {console.log ("fiesta!");} Else {console.log ("no taco!"}}}}). Call (this);   

And some unexpected did not output to Taco! So my question is two times why coffeescript does not check for undefined value and why is it strong?

What does the document say about ? :

The existence of the coffee script is the operator ? is not correct unless a variable null or undefined , which matches the value of the ruby ​​ zero? / Code>

Then it will definitely say "no taco":

  Taco = Taco if undefined? Console.log "Fiesta!" Else console.log "No Taco!"   

If your taco is clearly undefined then taco? is incorrect.

The coffee script announces the variable inherently, therefore, ? The javascript form depend on for example, if you only say so:

  if taco? Console.log "Fiesta!" Else console.log "No Taco!"   

Will you see that Taco? becomes typo taco! == "Undefined" & amp; Taco! == blank You can still check "this is zero " (in a tight form), but there is also a " var taco "; Note that typo taco exam also checks for taco = undefined , so a strict ! == The test can be used to see if taco is null .

You say this:

I have found that my version of coffeescript is only something! == Faucet

But it's not what it's doing, it's actually something! = Zero is compiled for ; Use the "sloppy" type changing the inequality (! == ) versus the inequality (! = ), which you claim ! = and ! The difference between == is important here:

  • Tap and undefined type == (but === ) No

    So if you know that the variable v has been declared (i.e. var v Is anywhere) then v is not null , it is enough to see that v! = Null neither undefined However, if you do not know that v has been announced, then you have a comparison of an undeclared variable with the null To avoid referencing, a typeof check is required . Consider this javascript:

      if (tako! = Null) {console.log ("fiesta!"); } And {console.log ("no taco!"); }   

    This will put a reference error in your face because taco does not exist:

      if (type of taco ! == "Undefined" & amp; amp; Taco! == faucet) console.log ("fiesta!"); } And {console.log ("no taco!"); }   

    On the other hand, check the guards for the investigation of typeof which is nothing which has not been declared. I do not think you can use Backlit to build the first in coffeehouse without embedding javascript.

No comments:

Post a Comment