javascript - HTML 注释 <!-- 是否充当 JavaScript 中的单行注释,为什么?

标签 javascript

JavaScript 具体如何理解构造 <!-- ?从JavaScript的 Angular 来看,它是不是//之外的又一个注释?和 /* */

从测试看来,JavaSript 对待<!--喜欢// : 一行

<script> <!-- alert('hi') //--> </script>

什么都不做,而

<script> <!-- 
alert('hi') //--> </script>

按预期工作。

此行为记录在何处?

这不是其他问题的重复:我不问为什么、是否或如何使用它。我正式地问它在 JavaScript 中有什么语法和语义。这个问题很重要,在其他问题中没有得到回答:例如,上面指出的行为不能从其他问题和他们的答案中猜到(实际上这是我的动机:我的程序没有像上面那样工作,而这些问题和答案无助于理解原因)。

最佳答案

From testing it seems that JavaSript treats <!-- like // a one-liner

是的,正如 ES6 规范中指定的那样,annex B :

B.1.3 HTML-like Comments

Comment ::
    MultiLineComment
    SingleLineComment
    SingleLineHTMLOpenComment
    SingleLineHTMLCloseComment
    SingleLineDelimitedComment

SingleLineHTMLOpenComment ::
    <!-- SingleLineCommentCharsopt

但是,注意附件B的描述:

This annex describes various legacy features and other characteristics of web browser based ECMAScript implementations. All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. However, the usage of these features by large numbers of existing web pages means that web browsers must continue to support them. The specifications in this annex defined the requirements for interoperable implementations of these legacy features.

These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.

因此,这部分仅用于描述现有的“非官方”行为,一旦浏览器停止实现此行为,就会从规范中删除。

关于javascript - HTML 注释 &lt;!-- 是否充当 JavaScript 中的单行注释,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080642/

相关文章:

javascript - 将 <img> 元素分配为 javascript 变量的值

javascript - chrome 扩展多个选项卡和 chrome.runtime.connect

javascript - $ ("#input").val() 在使用 $.post() 时返回未定义

javascript - window.clearInterval 不起作用?

javascript - AJAX session 调用了两次。第二次不再显示警报

javascript - 为什么javascript不返回四舍五入的数字

Javascript 从对象获取键和值

javascript - JS 1-2k 变量使页面加载缓慢

javascript - Angular 、下划线、拔除数组、排序

javascript - 在 html 文档的开头声明变量