如何在 CoffeeScript 中发表评论? “/*这个*/”不起作用

发布于 2024-12-09 22:58:32 字数 559 浏览 0 评论 0原文

您可以通过哪些方式在 CoffeeScript 中进行评论?

文档说您可以使用三个哈希符号来启动和关闭注释块:

###
  Comments
  go
  here
###

我发现有时可以使用以下两种格式

`// backticks allow for straight-JavaScript,
 // but the closing backtick can't be on a comment line (I think?)
`

是否有更简单的方法在 CoffeeScript 中插入简短注释?

不要使用这种风格**

由于这种风格得到了很多浏览,我想强调的是,

/* Comment goes here */

/* 位于其自己的行上时,会产生数学错误。

正如 Trevor 在对该问题的评论中指出的那样,这是一个正则表达式不是评论!

In what ways can you comment in CoffeeScript?

The documentation say you can use three hash symbols to start and close a comment block:

###
  Comments
  go
  here
###

I've found that I can sometimes use the following two formats

`// backticks allow for straight-JavaScript,
 // but the closing backtick can't be on a comment line (I think?)
`

Are there a simpler way to insert short comments in CoffeeScript?

Do NOT use this style**

Since this is getting a lot of views, I want to emphasize that

/* Comment goes here */

produces a MATH error when the /* is on its own line.

As Trevor pointed out in a comment on the question, this is a regular expression, NOT a comment!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

意犹 2024-12-16 22:58:32

使用单个 # 符号

# like this

一个字符似乎很少;)

另外:

###
This block comment (useful for ©-Copyright info) also gets 
passed on to the browsers HTML /* like this! */
###

Use a single # sign

# like this

One character seems pretty minimal ;)

Also:

###
This block comment (useful for ©-Copyright info) also gets 
passed on to the browsers HTML /* like this! */
###
我纯我任性 2024-12-16 22:58:32

注释的主要方式是 sh/Perl/Ruby/... 风格的 # 注释:

# This comment goes to the end of the line
# and it won't appear in the "compiled"
# JavaScript version.

您使用 块样式 ### 注释 当您希望注释出现在 JavaScript 版本中时:

有时您希望将块注释传递给生成的 JavaScript。例如,当您需要在文件顶部嵌入许可标头时。块注释反映了heredocs的语法,并保留在生成的代码中。

因此,如果您从以下位置开始

###
PancakeParser is Public Domain
###

,您将在生成的 JavaScript 中看到以下 JavaScript 注释:

/*
PancakeParser is Public Domain
*/

The main way to comment is sh/Perl/Ruby/... style # comments:

# This comment goes to the end of the line
# and it won't appear in the "compiled"
# JavaScript version.

You use the block style ### comments when you want a comment to appear in the JavaScript version:

Sometimes you'd like to pass a block comment through to the generated JavaScript. For example, when you need to embed a licensing header at the top of a file. Block comments, which mirror the syntax for heredocs, are preserved in the generated code.

So if you start with

###
PancakeParser is Public Domain
###

then you'd get this JavaScript comment in the generated JavaScript:

/*
PancakeParser is Public Domain
*/
—━☆沉默づ 2024-12-16 22:58:32

小心###!如果您使用 ### 来分隔代码部分(就像我所做的那样),当该代码因此停止工作时,您会感到非常惊讶。

Beware of ###! If you use ### to separate sections of code (as I do) it's awfully surprising when that code stops working as a result.

独﹏钓一江月 2024-12-16 22:58:32

对于行中间的注释,请尝试:

if somecond `/* hi */` && some other cond, etc.

反引号使 CoffeeScript 将包含的文本按原样放入生成的 JavaScript 中。

For comments in the middle of a line, try:

if somecond `/* hi */` && some other cond, etc.

The backticks cause CoffeeScript to put the enclosed text into the generated JavaScript as is.

难得心□动 2024-12-16 22:58:32

不幸的是,CoffeeScript 与使用单个 # 字符引入的注释是否进入输出 JavaScript 不一致。例如:

# testme.coffee

fName = 'John'
lName = 'Smith'
fullName = "#{fName} #{lName}" # |||| $:
if true #comment
    console.log `/* hi */` 'Hello, World!'

编译为

// Generated by CoffeeScript 2.7.0
(function() {
  // testme.coffee
  var fName, fullName, lName;

  fName = 'John';

  lName = 'Smith';

  fullName = `${fName} ${lName}`;

  if (true) { //comment
    console.log(/* hi */`Hello, World!`);
  }

}).call(this);

注意分配给 fullName 的行上缺少的注释。

Unfortunately, CoffeeScript is inconsistent with whether comments introduced with a single # character make it to the output JavaScript. For example:

# testme.coffee

fName = 'John'
lName = 'Smith'
fullName = "#{fName} #{lName}" # |||| $:
if true #comment
    console.log `/* hi */` 'Hello, World!'

compiles to

// Generated by CoffeeScript 2.7.0
(function() {
  // testme.coffee
  var fName, fullName, lName;

  fName = 'John';

  lName = 'Smith';

  fullName = `${fName} ${lName}`;

  if (true) { //comment
    console.log(/* hi */`Hello, World!`);
  }

}).call(this);

Note the missing comment on the line that assigns to fullName.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文