javascript - --> 在 JavaScript 中做什么?

标签 javascript comments

我无法在任何书籍、手册或网站中找到任何对这一声明的引用。据我所知,它的功能与 // 完全一样。评论。例如:

console.log("1");
--> console.log("2");
console.log("3");

将打印

1
3

我很好奇的是 --> 之间到底有什么区别?和 //是,如果存在的话,还有为什么 -->似乎完全没有出现在我见过的所有 JavaScript 引用资料中——我发现它的唯一原因是因为我不小心将它输入了 Adob​​e Dreamweaver,并且它被突出显示为注释。谢谢!

编辑:Here is a functional jsFiddle demonstrating the behavior .

编辑 2:我进一步测试,发现了一些东西。

  • 这仅在恰好有两个破折号的情况下有效。 ->--->会抛出错误。
  • 这只适用于“大于”符号。 --<会抛出错误。
  • 这在一行的中间或末尾不起作用。 console.log("1"); --> console.log("2");会抛出错误。

最佳答案

我的猜测是 JavaScript 引擎一如既往地宽容,默默地忽略了这一行,因为它看起来像是 HTML 注释的结尾 (<!-- -->)。 HTML 中内联的 JavaScript 历来被包裹在 HTML 注释中,这样不支持 JavaScript 的浏览器就不会尝试解析它。


编辑:

我做了一些调查,确实是这样。

来自 V8 的 scanner.cc :

If there is an HTML comment end '-->' at the beginning of a line (with only whitespace in front of it), we treat the rest of the line as a comment. This is in line with the way SpiderMonkey handles it.

来自 Rhino 的 Comment.java :

JavaScript effectively has five comment types:

  1. // line comments
  2. /* block comments */
  3. /** jsdoc comments */
  4. <!-- html-open line comments
  5. ^\s*--> html-close line comments

The first three should be familiar to Java programmers. JsDoc comments are really just block comments with some conventions about the formatting within the comment delimiters. Line and block comments are described in the Ecma-262 specification.

请注意 -->注释不是 Ecma-262 规范的一部分。

关于javascript - --> 在 JavaScript 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21807783/

相关文章:

java - 在 Java 中注释多行的语法是什么?

python - 丑陋的代码 : Amusing comment?

java - 类和方法之间注释中的 # 是什么意思?

javascript - jQuery - 将 div 从中心增长到动画

javascript - 网站的 Kiosk 模式

相当于 vbscript Now 和 Time() 的 Javascript

javascript - 如何使 ng-click 在 Angular DataTables 列生成器中工作?

javascript - PHP/HTML : Download PDF link working on page load

git - .gitignore 中的评论?

C# 隐藏和取消隐藏评论