HTML注释<! - 在JavaScript中充当单行注释,为什么? [英] Does HTML comment <!-- act as a single-line comment in JavaScript, and why?

查看:195
本文介绍了HTML注释<! - 在JavaScript中充当单行注释,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript如何理解构造<! - ?从JavaScript的角度来看,除了 // / * * / 之外,还有其他评论吗?

How specifically does JavaScript understand the construct <!--? From the point of view of JavaScript, is it yet another comment in addition to // and /* */?

从测试开始,JavaSript似乎将<! - 视为 // :单行

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

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

什么都不做,而

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

按预期工作。

在哪里记录这种行为?

Where is this behavior documented?

这不是其他问题的重复:我不会问为什么,是否或如何使用它。我正式问它在JavaScript中有什么语法和语义。这个问题非常重要,在其他问题中没有得到回答:例如,上面提到的行为无法从其他问题及其答案中猜到(实际上这是我的动机:我的程序如上所述没有单行程工作,那些问题和答案没有帮助理解为什么)。

This is not a duplicate of other questions: I don't ask why or whether or how it should be used. I ask what syntax and semantics it has in JavaScript, formally. The question is non-trivial and is not answered in other questions: for example, the behavior indicated above cannot be guessed from the other questions and their answers (in fact this was my motivation: my program with a one-liner as above did not work, and those questions and answers were no help in understanding why).

推荐答案


从测试它似乎JavaSript将<! - 视为 // 一行

是的,按照ES6规范的规定,附件B

Yes it does, as specified in the ES6 spec, annex B:


B.1.3类似HTML的评论

Comment ::
    MultiLineComment
    SingleLineComment
    SingleLineHTMLOpenComment
    SingleLineHTMLCloseComment
    SingleLineDelimitedComment

SingleLineHTMLOpenComment ::
    <!-- SingleLineCommentCharsopt


但是,请注意附录B的描述:

However, note the description of annex B:


本附件描述了基于Web浏览器的ECMAScript实现的各种遗留功能和其他特性。本附录中指定的所有语言功能和行为都具有一个或多个不良特征,并且在没有遗留用法的情况下将从本规范中删除。但是,大量现有网页对这些功能的使用意味着Web浏览器必须继续支持它们。本附件中的规范定义了这些遗留特性的可互操作实现的要求。

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.

这些特性不被视为核心ECMAScript语言的一部分。编写新的ECMAScript代码时,程序员不应使用或假设存在这些特性和行为。不鼓励ECMAScript实现实现这些功能,除非实现是Web浏览器的一部分或者需要运行与Web浏览器相同的旧ECMAScript代码。

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.

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

So, this part only exists to describe existing "unofficial" behavior and as soon as browsers stop implementing this behavior, will be removed from the spec.

这篇关于HTML注释&lt;! - 在JavaScript中充当单行注释,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆