在Javascript中,哪种结构会使用注释分隔符*/?

6

来自"Javascript: the Good Parts": the Good Parts

避免使用/* … */进行注释,因为*/在javascript中出现在有用的结构中。请使用//

我很好奇这些“有用的结构”是什么,因为我想不出任何反例(除了像/.*/这样的正则表达式?)


正则表达式字面量和字符串字面量。 - I Hate Lazy
人们可能会抱有希望,但最终可能会失望。 - I Hate Lazy
如果是这种情况,那么如果您始终使用RegExp构造函数,则可以安全地使用/*…*/进行注释。 - RobG
@user1689607:哎呀,那有点恶心。在我看来,这是一个合理的答案。 - nneonneo
2个回答

2
是的,他很可能指的是正则表达式。

2

博主试图缩写Crockford在《JavaScript: The Good Parts》第2章(语法)中所写的内容。这个话题出现在第一节(空格)的最后一段。它说:

The /* */ form of block comments came from a language called PL/I. PL/I chose those strange pairs as the symbols for comments because they were unlikely to occur in that language’s programs, except perhaps in string literals. In JavaScript, those pairs can also occur in regular expression literals, so block comments are not safe for commenting out blocks of code. For example:

 /* 
     var rm_a = /a*/.match(s); 
 */ 

causes a syntax error. So, it is recommended that /* */ comments be avoided and // comments be used instead. In this book, // will be used exclusively.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接