Issue #221 (JS Comments, Productivity, Frameworks, Testing)10/12/17
If you've been coding JavaScript since the early days of the web, then you might recall writing inline scripts like this in your HTML:
<script language="javascript">
<!--
if (document.all) {
// do something...
}
// -->
</script>
|
Notice the HTML comments around the JavaScript. This was done to ensure that browsers that didn't understand JavaScript would view this as nothing but an HTML comment. You could say it was a very early version of graceful degradation.
But maybe you didn't know that the ES6 specification has actually standardized HTML-like comments, for the purpose of ensuring backwards compatibility with older pages that might contain this kind of code. You can see this described
in the spec here. You can also see it described in the HTML5 spec in a
sub-section covering scripting.
So technically it's valid to include HTML comments in your JavaScript. You can try it out
in this JSFiddle.
Notice a couple of lines in the JavaScript with HTML-like comments. I don't see any kind of error in the console in the latest Chrome, Firefox, or Edge so it seems to work fine as long as the comments are single-line comments.
As mentioned, browsers already supported this behaviour and the spec added it to ensure compatibility with older pages,
which it explains at the start of the section where this feature is discussed:
"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."
It's unlikely you'll ever use HTML comments in your JavaScript, and you probably shouldn't ever use them but definitely a nice thing to know in case you weren't familiar with that old-school type of scripting.
Now on to this week's tools!