使用 Google 的闭包编译器缩小字符串的正确方法是什么?

发布于 2024-11-27 01:13:33 字数 526 浏览 1 评论 0原文

我正在编写 jQuery 插件,我想通过用枚举替换常用的 CSS 属性字符串来缩小脚本的大小。然而,Google 的闭包编译器将所有字符串变量替换为字符串文字。例如,选择高级优化:

var x = "hey bob how are you doing";
alert(x);
alert(x);
alert(x);
alert(x);

将返回

alert("hey bob how are you doing");alert("hey bob how are you doing");alert("hey bob how are you doing");alert("hey bob how are you doing");

What is the right way to do that I'm try to do without getting my code through a stringcompressor like JScrambler?

提前致谢。

I'm in the middle of writing a jQuery plugin, and I'd like to shrink the size of my script by replacing commonly used CSS property strings with enums. However, Google's Closure Compiler replaces all string variables with string literals. For example, with advanced optimization selected:

this

var x = "hey bob how are you doing";
alert(x);
alert(x);
alert(x);
alert(x);

returns

alert("hey bob how are you doing");alert("hey bob how are you doing");alert("hey bob how are you doing");alert("hey bob how are you doing");

What is the right way to do what I'm trying to do without sending my code through a string compressor like JScrambler?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

花开柳相依 2024-12-04 01:13:33

Stephen Chung的回答(所以这个问题可以显示为已回答):

扩展版本减少了 gzipped 大小。编译器正在做
正确的做法是最小化 gzip 压缩的下载大小并加快下载速度
通过消除变量来编写脚本。有一个 aliasAllStrings 标志
这将强制字符串别名——本质上是创建一个
每个字符串的变量。

Stephen Chung's answer ( so this question can show as answered ):

The expanded version reduces gzipped-size. The compiler is doing the
right thing to minimize the gzipped download size and to speed up the
script by eliminating a variable. There is an aliasAllStrings flag
that will force aliasing of strings -- essentially creating one
variable for each string.

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