JavaScript中的空白何时重要? [英] When does whitespace in JavaScript matter?

查看:73
本文介绍了JavaScript中的空白何时重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在考虑代码最小化。我可以想到几个地方

空白很重要


a + ++ b

a ++ + b

a - --b

a-- -b

当一行没有分号结束时,新行

角色很重要。


其他人吗?


谢谢,

彼得

Hi,

I''m thinking about code minimization. I can think of a few places where
whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.

Any others?

Thanks,
Peter

推荐答案

Peter Michaux在1/8/2007 2:58 PM发表以下内容:
Peter Michaux said the following on 1/8/2007 2:58 PM:

Hi ,


我正在考虑代码最小化。我可以想到一些地方

空白问题
Hi,

I''m thinking about code minimization. I can think of a few places where
whitespace matters



在这四个空格很重要的实例中我总是把它包裹在

用于删除空白问题的括号。

In those 4 instances where whitespace will matter I always wrap it in
parentheses to remove the whitespace issue.


a + ++ b

a ++ + b

a - --b

a-- -b

当一行没有分号结尾时,新行

字符事项。
a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.



虽然并非总是如此:


函数someFunction()

{


那里的换行符无关紧要。但是,如果你用; \ n替换所有新的

行,那么你就破坏了代码。换行符和分号是一个

的地方,你必须手动或写一个JS解析器。


另一个例子,换行符不能简单地更换,但

换行无关紧要:


for(i in something)

while(yourWifeIsntPregnant)


无论{在同一行还是下一行都没关系,

换行仍然无关紧要但是最后没有分号。


你不能简单地说之后不要加分号。无论是。想想

匿名函数....()

你可以查看)看看它是否是()或者)它仍然可以让你进入
没有参数的函数
麻烦:


函数myFunction()

{


}


如果换行符不能替换为换行符,但如果

这是一个全局匿名函数,你想删除换行符然后

你*必须*有分号或者它会破坏代码。


var x =(function(){

/ /某些代码在这里

})();


无法删除分号和换行符,必须在那里。


分号和换行将成为你的新噩梦:)


-

兰迪

机会有利于准备好的思想

comp.lang.javascript常见问题 - http ://jibbering.com/faq/index.html

Javascrip t最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


" Peter Michaux" < pe ********** @ gmail.comwrote:
"Peter Michaux" <pe**********@gmail.comwrote:




我'我在考虑代码最小化。我可以想到几个地方

空白很重要


a + ++ b

a ++ + b

a - --b

a-- -b

当一行没有分号结束时,新行

性格很重要。
Hi,

I''m thinking about code minimization. I can think of a few places where
whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.



新行字符在某些时候才有意义。不幸的是,如果没有完整的解析器,你不会知道它是否重要。

The new line character only matters some of the time. Unfortunately you
aren''t going to know whether or not it matters without doing a full parser.


>

其他任何人?
>
Any others?



一个关键字后跟一个字母或数字:return 42; vsreturn42;


内部字符串和正则表达式。


紧跟在一个部门后面的评论:

a = x / / *最好不要为零:* / y;

z = 1;

vs.:

a = x // *最好不要为零:* / y;

z = 1;

A keyword followed by a letter or digit: "return 42;" versus "return42;"

Inside strings and regular expressions.

A comment which immediately follows a division:
a = x / /*this had better not be zero: */y;
z = 1;
versus:
a = x //*this had better not be zero: */y;
z = 1;


Randy Webb写道:
Randy Webb wrote:

Peter Michaux在1/8/2007 2:58 PM发表以下内容:
Peter Michaux said the following on 1/8/2007 2:58 PM:




我正在考虑代码最小化。我可以想到几个地方

空白问题
Hi,

I''m thinking about code minimization. I can think of a few places where
whitespace matters



< snip>

<snip>


分号和换行符将成为你的新噩梦:)
Semicolons and newlines will become your new nightmare :)



我想是这样。我可能只是决定留下新的线路。


这是更多的评论,空白行和开始或中线空间

是更大的担忧。我一直在使用jslint在jsmin之前检查

问题,但jslint有许多程序员偏好构建

in而不仅仅是在最小化时会有害的直接问题。

我想我可以剥掉jslint。


我真正想要的是一个命令我可以在一个目录中输入

检查是否该目录中的javascript文件是可以最小化的,如果

,那么最小化它们。在该步骤之后,所有文件都被gzip压缩。这个

将为Apache节省为每个

单个文件请求使用mod_deflate的麻烦。我不知道为什么他们没有为mod_deflate建立缓存

但是因为我必须做最小化步骤

无论如何我还可以捎带压缩步骤在同一个命令上。

我确实知道一件事:我不想做所有这些

手工部署的东西从 http://jslint.com 。


Peter

I imagine so. I may just decide to leave new lines in place.

It''s more the comments, blank lines and beginning or mid-line spaces
that are a bigger concern. I have been using jslint to check for
problems before jsmin but jslint has many programmer preferences built
in and not just outright problems that will be harmful when minimizing.
I suppose I could strip jslint down.

What I really want is a command I can type inside a directory that
checks if the javascript files in that directory are minimizable and if
so minimizes them. Following that step all the files are gzipped. This
will save Apache the trouble of having to use mod_deflate for every
single request for a file. I don''t know why they didn''t build caching
for this into mod_deflate but since I have to do the minimize step
anyway I can also piggy back the compression step on the same command.
I know one thing for sure: I don''t want to have to do all this
deployment stuff by hand starting with http://jslint.com anymore.

Peter


这篇关于JavaScript中的空白何时重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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