javascript 可以有多少行?
我正在为 javascript 编写一个小的自定义缩小器,我想知道是否需要在单行上一定数量的字符后强制换行,或者它是否重要?
例如,我可以将整个脚本缩小为包含数千个字符的单行,或者每 n 个字符,我可以插入一个换行符(在 js 代码中可接受的点)以将其放到下一行。
是否有这样做的理由,或者 javascript/浏览器不关心一行中有多少个字符?
I am writing a little custom minifier for javascript, and I am wondering if I need to force line breaks after a certain number of characters on a single line, or if it matters at all?
For example, I can minify an entire script into a single line with thousands of characters, or every n characters, I could insert a line break (at an acceptable point in the js code) to drop it to the next line.
Is there a reason to do this, or is does javascript/browsers not care how many characters are on a line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不想把我的网站押在各种网络服务器、代理、Internet Explorer 等上,所有这些都不会在巨大行长度上令人窒息。当然,我不太清楚如何定义巨大,但是 jQuery - 作为一个例子 - 看起来它限制为每行大约 512 个字符。这么说吧,如果你的缩小器允许我选择是否每 x 个字符换行一次,我更喜欢你的缩小器。
I wouldn't want to bet my site on various web servers, proxies, Internet Explorer, etc. all not-choking on huge line lengths. Of course, I don't know quite how I'd define huge, but jQuery - as an example - looks like it limits to about 512 characters per line. Put it this way, I'd prefer your minifier if it allowed me to choose whether I have line breaks every x characters or not.
长行 JavaScript 存在一些陷阱。以下内容摘自闭包编译器的常见问题解答(此链接):
There are some pitfalls regarding to long lines of JavaScript. The following is from the Closure Compiler's FAQ (this link):
您可以将整个脚本放在一行上,这对解析器没有影响。
然而,将内容分成大约 4kB 的行可能对下载程序有利,可以稍微提高性能。
You can put an entire script on one line, it makes no difference to the parser.
However breaking content onto lines of about 4kB may be beneficial to the downloader, slightly improving performance.