更新时间:2021-12-08 01:37:28
您正试图超越编译器.这是一场失败的战斗.但是,这是人们尝试做这种事情的两个主要原因.
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.
减小代码的大小.理论上是单个字母变量小于关键字this
.但是,这种理论在大多数情况下是有缺陷的.请参见编译器常见问题解答.
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;
})()