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

How do I tell the closure compiler to ignore code?

本文关键字:代码 编译器 何告诉 闭包      更新时间:2023-09-26

我决定在实现接口时需要一些东西来帮助我。所以我在闭包库的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.implements = function (ctor, interfaceCtor) {
  if (goog.DEBUG) {
    ...
  }
};

如果这些不合适,你可以自己定义。

或者你的意思完全是别的?