1

如果通过高级优化运行以下代码,我仍然可以在代码中看到调试语句。经过高级模式编译使用Google Closure Compiler去除调试代码

(function() { 
     var a = console.info.bind(console), 
      b = { 
       max: 100, 
       debug: !1 
      }; 
     b.debug && a("This should be in debug mode only"); 
     "function" == typeof alert && alert(b); 
     a("Brady", Math.random() * b.max | 0); 
    })(); 

如何才能摆脱调试信息与先进模式

var log = console.info.bind(console); 
    (function() { 
     /** @const */ 
     var DEBUG = false; 

     log('Brady', createRank({ 
      max: 100, 
      debug: DEBUG 
     })); 
    })(); 
function createRank(options) { 
    if (options.debug) { 
     log('This should be in debug mode only'); 
    } 
    if(typeof alert == 'function'){ 
     alert(options); 
    } 
    return (Math.random() * options.max) | 0; 
} 

输出?

如果调试变量被定义为全局的,和日志记录语句被封入等

如果(DEBUG){ 日志( '调试消息'); }

那么它会工作,但有没有办法让它工作,如果我们不希望它作为一个全局变量,而是通过参数传递给各个模块/函数。

回答

0

这是当前优化集以及它们运行时的限制。优化是编译时间和优化之间的折衷,所做的选择对于每种代码模式都不一定是理想的。

在这种特殊情况下,问题是“属性折叠”只发生一次全局作用域,并且发生在函数内联之前(主要优化循环期间发生函数本地对象的“属性折叠”)。为了将示例中的代码删除,“折叠属性”需要至少运行一次,或者需要增强功能本地版本(更保守)以在全局范围内运行。

这也是在这里讨论:https://github.com/google/closure-compiler/issues/891

+0

看起来这是接近我们会得到回答的问题 – sbr 2015-04-01 22:41:25

相关问题