简体   繁体   English

Closure Compiler为什么不缩短这个?

[英]Why does Closure Compiler not shorten this?

I'm not sure whether this is just a bug or an intended feature. 我不确定这只是一个bug还是一个预期的功能。

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): 基本上,我有这个小功能(我现在看到这里的end是蓝色的,但是这很好用,如果我将它重命名为其他东西我还有问题):

function f(a, b) {
    var start = Math.min(a, b);
    var end = Math.max(a, b);

    tb.selectionStart = start;
    tb.selectionEnd = end;
};

When closure-compiling it, I get: 关闭编译时,我得到:

function f(a,b){var c=Math.max(a,b);tb.selectionStart=Math.min(a,b);tb.selectionEnd=c};

However, why is selectionStart set to Math.min directly, whilst selecitonEnd is set to a variable ( c ), which is declared first? 但是,为什么selectionStart直接设置为Math.min ,而selecitonEnd设置为变量( c ),首先声明? Isn't it shorter to do tb.selectionEnd=Math.max(a,b) ? tb.selectionEnd=Math.max(a,b)不是更短吗?

Any ideas are appreciated. 任何想法都表示赞赏。

EDIT: 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 编辑:在这个链接中有一个“官方”答案: 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. 但是,如果中间有任何声明无法证明没有副作用,那么编译器就不会内联它。

In your case, assignment to variable "start" is separated from the usage of "start" only by the assignment statement to "end". 在您的情况下,变量“start”的赋值与“start”的使用仅通过赋值语句分离为“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. 但是,这个语句没有副作用,因为Math.max是一个内部函数,编译器知道它是无副作用的。

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. 但是,在您的情况下,通过语句将对变量“end”的赋值与该变量的用法分开,该语句是对属性的“start”赋值。 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). 这是因为某些属性在分配时实际上会导致不同的行为,或者更改全局状态(例如RegExp)。 In some systems, property assignments actually trigger certain system-specific features (eg 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;

The compiler won't eliminate the first two statements since assignment to "bar" may have side effects. 编译器不会消除前两个语句,因为赋值给“bar”可能会产生副作用。

So, in your question, the variable "end" cannot be inlined because the statement tb.selectionStart = start; 所以,在你的问题中,变量“end”不能内联,因为语句tb.selectionStart = start; may have side effects (perhaps only in wierd cases). 可能有副作用(可能只在奇怪的情况下)。

If you make "tb" a local variable, or something that the compiler has complete control of (eg a simple object: var tb = {}; ), then you'll find that the compiler inlines all of the assignments just fine. 如果你使“tb”成为局部变量,或者编译器完全控制的东西(例如一个简单的对象: var tb = {}; ),那么你会发现编译器很好地内联了所有的赋值。

if you paste this code, this works. 如果你粘贴这个代码,这是有效的。

function f(a, b) {
    var start = Math.min(a, b);
    tb.selectionStart = start;

    var end = Math.max(a, b);
    tb.selectionEnd = end;
};

function f(a,b){tb.selectionStart=Math.min(a,b);tb.selectionEnd=Math.max(a,b)};

i this is a mistake by closure compiler. 我这是封闭编译器的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM