评论100%的安全在所有主要环境? [英] Are comments 100% safe on all the major environments?

查看:96
本文介绍了评论100%的安全在所有主要环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解析之前,注释是否真的从您的来源中删除,或者它们可能会被视为换行符并破坏某些上下文中的连续性?

 'foo'.replace(/ f /,'b')// f-> b 
.replace(/ o /,'a')// o-> a
。 replace(/ o /,'r')/ * o-> r * /;

'foo'.replace(/ x /,/ * matchesx* /'y');

var foo =(true!== false)? // bikeshed
'bar':/ *如果他们是相等的,他们不会,我们会想要'baz'* /'baz';

你知道,因为他们说空格是安全的和无关紧要的知道有例外。 ,如果它们在解析之前被剥离,解析器将需要扫描输入两次。


但是,LineTerminator线不被认为是
是单行注释的一部分;它分别由
识别词法语法,并成为语法语法的输入元素流
的一部分。这一点非常重要,因为它
意味着存在或不存在单行注释不会影响
自动分号插入的过程。


ES5规格



源代码被标记为假如注释不存在。


Are comments literally just stripped out of your source before parsing, or can they count as linebreaks and disrupt continuity in certain contexts?

'foo'.replace(/f/, 'b') //f->b
     .replace(/o/, 'a') //o->a
     .replace(/o/, 'r') /*o->r*/ ;

'foo'.replace(/x/, /*matches "x"*/ 'y');

var foo = ( true !== false ) ? // bikeshed
          'bar' : /*if they're equal, which they won't be, we'll want 'baz'*/ 'baz';

You know, cause they say whitespace is "safe" and "insignificant" or whatever, but we all know there are exceptions to that. Are comments actually safe?

解决方案

They're ignored during parsing, if they were stripped out before parsing, the parser would need to scan the input twice.

However, the LineTerminator at the end of the line is not considered to be part of the single-line comment; it is recognised separately by the lexical grammar and becomes part of the stream of input elements for the syntactic grammar. This point is very important, because it implies that the presence or absence of single-line comments does not affect the process of automatic semicolon insertion

ES5 Specification for comments.

The source code is tokenised as if the comments didn't exist.

这篇关于评论100%的安全在所有主要环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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