質問

My code is made of a few files and the default value may be overriden at initialization. By default, Google Closure Compiler will keep initializing with the default value though it is never used. Is there an equivalent to @nosideeffects so that the first variable definition is dropped.

Here is an example:

var a = 1;
a = 2;
window.console.log(a);

Will be compiled into (in advanced mode):

var a=1,a=2;window.console.log(a);

In my case, the default value is a pretty long string and I feel it's worth finding a way to tell the compiler to only keep the latest.

役に立ちましたか?

解決

The Closure Compiler has several optimization that only run within function bodies (because they are too expensive to run on large projects): "flow sensitive inlining" is the one you want here. In simple mode, this:

function f() {
  var a = 1;
  a = 2;
  window.console.log(a);
}

becomes:

function f() {
  window.console.log(2);
}

There isn't anything you need to do to enable this except don't put your code in global scope.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top