为什么Google Closure Compiler不会内联这个中间变量赋值?

时间:2016-10-12 19:49:05

标签: javascript minify google-closure-compiler

我在高级模式下使用Google Closure Compiler。我有一段代码将全局函数的返回值赋给变量,然后立即只使用变量的值一次。为简单起见,想象一下就是这个(try it yourself in the Closure Compiler Service):

(function () {
  var thingIWantToName = mysteriousGlobalFunction()
  console.log(thingIWantToName)
})()

我希望将代码缩小为类似的东西,图表A:

console.log(mysteriousGlobalFunction())

相反,它会缩小为保留中间var声明的代码,图表B:

var a=mysteriousGlobalFunction();console.log(a);

我知道Closure Compiler不能保证mysteriousGlobalFunction()没有看到实现就没有副作用,也不能知道它不依赖于全局状态。我可以理解为什么它不会删除变量赋值if there was another statement in between,或者是否再次使用变量。

但是在这种情况下,我很难看到在mysteriousGlobalFunction()中可能会出现什么样的表现A!==图表B,在执行顺序或其他方面。有什么建议吗?

0 个答案:

没有答案
相关问题