gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:52:34 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com