CoffeeScript是否允许JavaScript风格的==等价语义? [英] Does CoffeeScript allow JavaScript-style == equality semantics?

查看:85
本文介绍了CoffeeScript是否允许JavaScript风格的==等价语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 CoffeeScript将 == 编译到JavaScript = == 运算符。但是如果你想要原始的JS == 语义怎么办?它们可用吗?我已经累了的文档,并找不到任何启用这个。

I love that CoffeeScript compiles == into the JavaScript === operator. But what if you want the original JS == semantics? Are they available? I've pored over the documentation and can't find anything enabling this.

更普遍的是,有一种方法来内联纯文本到我的CoffeeScript代码,使编译器不接触它?

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

我希望避免编辑已编译的JavaScript输出,因为我使用 Chirpy 在Visual Studio中自动生成它。

I'd prefer to avoid editing the compiled JavaScript output, since I'm using Chirpy to auto-generate it in Visual Studio.

推荐答案


作为这个的一个可能的扩展,有没有一种方法将常规JS的块嵌入到CoffeeScript代码中,以便不编译?

As a possible extension to this, is there a way to inline blocks of regular JS into CoffeeScript code so that it isn't compiled?

是,这是文档。您需要将JavaScript代码封装在反引号中(`)。这是您在CoffeeScript中直接使用JavaScript的 == 的唯一方法。例如:

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

if `a == b`
  console.log "#{a} equals #{b}!"



编译的JavaScript

Compiled JavaScript

if (a == b) {
  console.log("" + a + " equals " + b + "!");
}






code> == null / 未定义 / void 0 postfix existsential operator


The specific case of == null/undefined/void 0 is served by the postfix existential operator ?:

x = 10
console.log x?



编译的JavaScript

Compiled JavaScript

var x;
x = 10;
console.log(x != null);





CoffeeScript Source []

CoffeeScript Source [try it]

# `x` is not defined in this script but may have been defined elsewhere.
console.log x?



编译的JavaScript

Compiled JavaScript

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

这篇关于CoffeeScript是否允许JavaScript风格的==等价语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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