Does CoffeeScript allow JavaScript-style == semantics of equality?

I like that CoffeeScript compiles == into a JavaScript === statement . But what if you want to use the original JS == semantics? Are they available? I looked through the documentation and can not find anything that could be done.

More generally, is there a way to embed plain JS in my CoffeeScript code so the compiler doesn't touch it?

I would prefer to avoid editing compiled JavaScript output, since I use Chirpy to automatically create it in Visual Studio.

+53
javascript coffeescript
Aug 11 '11 at 20:28
source share
2 answers

As a possible extension for this, is there a way to embed regular JS blocks in CoffeeScript code so that it doesn't compile?

Yes, here is the documentation . You need to wrap the JavaScript code in backticks ( ` ). This is the only way to directly use JavaScript == in CoffeeScript. For example:

CoffeeScript source [ try ]
 if `a == b` console.log "#{a} equals #{b}!" 
Compiled JavaScript
 if (a == b) { console.log("" + a + " equals " + b + "!"); } 



Is the specific case == null / undefined / void 0 served by the postfix extension operator ? :

CoffeeScript source [ try ]
 x = 10 console.log x? 
Compiled JavaScript
 var x; x = 10; console.log(x != null); 
CoffeeScript Source [ try ]
 # `x` is not defined in this script but may have been defined elsewhere. console.log x? 
Compiled JavaScript
 var x; console.log(typeof x !== "undefined" && x !== null); 
+74
Aug 11 '11 at 20:33
source share

This is not quite the answer, but this problem arose because of me because jQuery.text () included spaces, and "is" failed in Coffeescript. Work around this with jQuery's trim function:

 $.trim(htmlText) is theExpectedValue 
+1
Jan 05 '15 at 0:31
source share



All Articles