Javascript 如何告诉闭包编译器忽略代码?

Javascript 如何告诉闭包编译器忽略代码?,javascript,google-closure-compiler,google-closure,google-closure-library,Javascript,Google Closure Compiler,Google Closure,Google Closure Library,我决定在实现接口时需要一些帮助。因此,我将此函数添加到closure库中的base.js文件中 /** * Throws an error if the contructor does not implement all the methods from * the interface constructor. * * @param {Function} ctor Child class. * @param {Function} interfaceCtor class. */ go

我决定在实现接口时需要一些帮助。因此,我将此函数添加到closure库中的base.js文件中

/**
 * Throws an error if the contructor does not implement all the methods from 
 * the interface constructor.
 *
 * @param {Function} ctor Child class.
 * @param {Function} interfaceCtor class.
 */
goog.implements = function (ctor, interfaceCtor) {
    if (! (ctor && interfaceCtor))
    throw "Constructor not supplied, are you missing a require?";
    window.setTimeout(function(){
        // Wait until current code block has executed, this is so 
        // we can declare implements under the constructor, for readability,
        // before the methods have been declared.
        for (var method in interfaceCtor.prototype) {
            if (interfaceCtor.prototype.hasOwnProperty(method)
                && ctor.prototype[method] == undefined) {
                throw "Constructor does not implement interface";
            }
        }
    }, 4);
};
现在,如果我声明我的类实现了一个接口,但没有实现接口的所有方法,那么这个函数将抛出一个错误。从最终用户的角度来看,这绝对没有好处,它只是帮助开发人员的一个很好的补充。因此,当闭包编译器看到下面的行时,我如何告诉它忽略它

goog.implements(myClass, fooInterface);

是可能的吗?

这取决于你所说的忽略是什么意思。您希望它编译成零,这样它只在未编译的代码中工作吗?如果是,您可以使用标准@define值之一:

goog.implements = function (ctor, interfaceCtor) {
  if (!COMPILED) {
    ...
  }
};
或者,仅当启用goog.DEBUG时:

goog.implements = function (ctor, interfaceCtor) {
  if (goog.DEBUG) {
    ...
  }
};
如果这些不合适,你可以自己定义


或者你的意思是完全其他的吗?

为什么不在AO上运行一个生产版本,让编译器给你这个反馈呢?希望AO完全去除不需要的代码。这样就可以了,我只是在它编译后没有在函数中执行循环和其他代码。对于用户来说,它的速度稍微加快了一点。谢谢