Google Closure编译器:如何保留缓存​​"this"的代码? [英] Google Closure Compiler: How to preserve code that caches "this"?

查看:85
本文介绍了Google Closure编译器:如何保留缓存​​"this"的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用Google Closure Compiler压缩我的JavaScript文件.现在,它似乎可以很好地压缩我的代码.

I use Google Closure Compiler frequently for compressing my JavaScript files. Now, it seems to compress my code fairly well.

现在,我尝试养成将this对象存储在本地变量中的习惯,因为不能混淆this,但是可以混淆本地变量.但是,Google Closure Compiler无法识别这一点,而是删除了局部变量的所有实例,将其替换为this.

Now, I try to make a habit of storing the this object in a local variable, because this cannot be obfuscated, but a local variable certainly can be obfuscated. However, Google Closure Compiler does not recognize this, and instead removes all instances of the local variable, replacing it with this.

我很清楚,编写代码时应避免预优化.但是,我认为缓存this是可以接受的,因为这样做可以使内容更清晰(因为this可以具有许多上下文,并且用另一个名称引用它可以减少歧义).

I am well aware that one should avoid pre-optimization when writing code. However, I feel that caching this is acceptable because doing so provides clarity (because this can have many contexts, and referencing it by another name will reduce ambiguity).

下面的代码非常基本,我知道它的编写可能不正确.但是,该代码将准确演示我所面临的问题.

The code below is pretty basic, and I understand that it may be poorly written. However, the code will demonstrate exactly the issue which I am facing.

这是压缩前的原始源文件:

Here is the original source file, before compression:

(function() {
  var that = this;
  that.a = 3;
  that.b = 4;
  this.c = 5;
  return that;
}());

现在是压缩的源文件.请注意,thisthat的分配已删除.

Now here is the compressed source file. Note that the assignment of this to that has been removed.

(function(){this.a=3;this.b=4;this.c=5;return this})();

理想情况下,我希望对that的分配保持某种形式,也许与此类似:

Ideally, I would expect the assignment to that to remain in some form, perhaps something similar to this:

(function(){var t=this;t.a=3;t.b=4;t.c=5;return t})();

现在,上面的代码几乎不保存任何字节,但是当使用更大的脚本时(如我经常做的那样),节省的费用肯定会加起来.

Now, the code above hardly saves any bytes, but when working with a much larger script (as I often do), the savings most definitely add up.

简而言之,如何防止Closure Compiler在上面的脚本中删除that变量?

In short, how can I prevent the Closure Compiler from removing the that variable in my above script?

推荐答案

您正试图超越编译器.这是一场失败的战斗.但是,这是人们尝试做这种事情的两个主要原因.

You are attempting to out-think the compiler. It's a losing battle. However, here's the two main reasons people try to do this type of thing.

  1. 减小代码的大小.理论上是单个字母变量小于关键字this.但是,这种理论在大多数情况下是有缺陷的.请参见编译器常见问题解答.

  1. Reduce size of code. The theory being that a single letter variable is smaller than the keyword this. However, this theory is flawed in most cases. See the compiler FAQ.

防止关键字this的上下文更改.但是,在SIMPLE_OPTIMIZATIONS中这是不必要的.如果创建引用变量的内部闭包,则编译器将不会内联该值.在ADVANCED_OPTIMIZATIONS下,在原型函数或构造函数之外使用关键字this可能很危险,因此应谨慎行事.请参见解释原因的文章.

Prevent the context of the keyword this from changing. However, in SIMPLE_OPTIMIZATIONS this is unnecessary. If you create an inner closure that references your variable, the compiler will not inline the value. Under ADVANCED_OPTIMIZATIONS, using the keyword this can be dangerous outside of a prototype function or constructor and should be done with care. See an article explaining why.

如果您确实想防止编译器内联您的值,则需要使用加引号的语法将其作为属性添加到对象上:

If you really want to prevent the compiler from inlining your value, you'll need to add it as a property on an object using quoted syntax:

(function() {
  var config = {};
  config['that'] = this;
})()

这篇关于Google Closure编译器:如何保留缓存​​"this"的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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