问题描述
我不确定这只是一个错误还是一个预期的功能。
I'm not sure whether this is just a bug or an intended feature.
基本上,我有这个小功能(我现在看到 end
在这里是蓝色的,但是这很好用,如果我将它重命名为其他东西我还有问题):
Basically, I have this tiny function (I now see end
is colored blue here but this works just fine, if I rename it to something else I still have the issue):
function f(a, b) {
var start = Math.min(a, b);
var end = Math.max(a, b);
tb.selectionStart = start;
tb.selectionEnd = end;
};
关闭编译时,我得到:
function f(a,b){var c=Math.max(a,b);tb.selectionStart=Math.min(a,b);tb.selectionEnd=c};
但是,为什么 selectionStart
设置为 Math.min
直接,而 selecitonEnd
设置为变量( c
),首先宣布?是不是更短 tb.selectionEnd = Math.max(a,b)?
However, why is selectionStart
set to Math.min
directly, whilst selecitonEnd
is set to a variable (c
), which is declared first? Isn't it shorter to do tb.selectionEnd=Math.max(a,b)
?
任何想法非常感谢。
推荐答案
编辑:此链接中有官方回答:
THERE IS AN "OFFICIAL" ANSWER IN THIS LINK:https://web.archive.org/web/20151226143155/http://code.google.com/p/closure-compiler/issues/detail?id=410
我认为可以内联对变量的赋值,紧接着使用该变量。但是,如果中间有任何声明无法证明没有副作用,那么编译器就不会内联它。
I think an assignment to a variable, followed immediately by usage of that variable, can be inlined. However, if there is any statement in between that cannot be proven to be free of side-effects, then the compiler won't inline it.
在你的情况下,赋值变量start仅与赋值语句end的start的使用分开。但是,这个语句没有副作用,因为Math.max是一个内部函数,编译器知道它是无副作用的。
In your case, assignment to variable "start" is separated from the usage of "start" only by the assignment statement to "end". However, this statement is free of side-effects since Math.max is an internal function and the compiler knows that it is side-effect-free.
然而,在你的case,对变量end的赋值通过语句与该变量的用法分开,该语句是对属性的start赋值。现在,我相信编译器并不认为仅仅分配属性总是没有副作用;这是因为某些属性在分配时实际上会导致不同的行为,或者更改全局状态(例如RegExp)。在某些系统中,属性分配实际上会触发某些特定于系统的功能(例如硬件接口),这些功能可能会反过来包含副作用。
However, in your case, assignment to variable "end" is separated from the usage of that variable by a statement, which is an assignment of "start" to a property. Now, I believe that the compiler does not assume that merely assigning to a property is always side-effect-free; that is because some properties, when assigned, actually cause different behavior, or change global state (such as RegExp). In some systems, property assignments actually trigger certain system-specific features (e.g. hardware interface) that may in-turn contain side-effects.
这就是为什么,有时,何时你有这样的代码:
That is why, sometimes, when you have code like this:
foo.bar = 1;
foo.bar = 2;
foo.bar = 3;
编译器不会消除前两个语句,因为赋值给bar可能会产生副作用。
The compiler won't eliminate the first two statements since assignment to "bar" may have side effects.
因此,在您的问题中,变量end无法内联,因为语句 tb.selectionStart = start;
可能有副作用(可能仅在奇怪的情况下)。
So, in your question, the variable "end" cannot be inlined because the statement tb.selectionStart = start;
may have side effects (perhaps only in wierd cases).
如果你使tb成为一个局部变量,或者编译器完全控制了(例如一个简单的对象: var tb = {};
),然后你会发现编译器内联所有的赋值就好了。
If you make "tb" a local variable, or something that the compiler has complete control of (e.g. a simple object: var tb = {};
), then you'll find that the compiler inlines all of the assignments just fine.
这篇关于Closure Compiler为什么不缩短这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!