javascript 可以有多少行?

发布于 2024-12-29 08:33:26 字数 196 浏览 0 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2025-01-05 08:33:26

我不想把我的网站押在各种网络服务器、代理、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.

爱的那么颓废 2025-01-05 08:33:26

长行 JavaScript 存在一些陷阱。以下内容摘自闭包编译器的常见问题解答(此链接):

为什么编译的脚本中有随机换行?

闭包编译器有意每 500 个字符左右添加换行符。防火墙和代理有时会损坏或忽略具有很长行的大型 JavaScript 文件。每 500 个字符添加换行符可以防止此问题。删除换行符对脚本的语义没有影响。对代码大小的影响很小,并且编译器优化了换行符位置,以便在对文件进行 gzip 压缩时,代码大小的损失甚至更小。

There are some pitfalls regarding to long lines of JavaScript. The following is from the Closure Compiler's FAQ (this link):

Why are there random line feeds in compiled scripts?

The Closure Compiler intentionally adds line breaks every 500 characters or so. Firewalls and proxies sometimes corrupt or ignore large JavaScript files with very long lines. Adding line breaks every 500 characters prevents this problem. Removing the line breaks has no effect on a script's semantics. The impact on code size is small, and the Compiler optimizes line break placement so that the code size penalty is even smaller when files are gzipped.

羅雙樹 2025-01-05 08:33:26

您可以将整个脚本放在一行上,这对解析器没有影响。

然而,将内容分成大约 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文