How do I comment in CoffeeScript? “/* this */” doesn't work

人盡茶涼 提交于 2019-11-28 19:24:16

问题


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!


回答1:


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! */
###



回答2:


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
*/



回答3:


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.



来源:https://stackoverflow.com/questions/7781685/how-do-i-comment-in-coffeescript-this-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!