CoffeeScript 是否允许 JavaScript 风格的 == 相等语义?

Does CoffeeScript allow JavaScript-style == equality semantics?(CoffeeScript 是否允许 JavaScript 风格的 == 相等语义?)

本文介绍了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.

更一般地说,有没有办法将纯 JS 内联到我的 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 + "!");
}

<小时>

== null/undefined/void 0 的具体情况由后缀存在运算符 ?:


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 源 [试试]

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 风格的 == 相等语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:CoffeeScript 是否允许 JavaScript 风格的 == 相等语义?

基础教程推荐