如何告诉闭包编译器忽略代码? [英] How do I tell the closure compiler to ignore code?

查看:107
本文介绍了如何告诉闭包编译器忽略代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在实现接口时需要一些帮助.因此,我将此功能添加到了闭包库的base.js文件中.

I decided I needed something to help me a bit while implementing interfaces. So I added this function to the base.js file in the closure library.

/**
 * 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);
};

现在,如果我声明我的类实现了一个接口但未实现所有接口的方法,则此函数将引发错误.从最终用户的角度来看,这绝对没有好处,它只是对开发人员的帮助一个不错的补充.因此,我如何告诉闭包编译器在看到它时忽略下一行?

Now this function will throw an error if I declare that my class implements a interface but doesn't implement all the interface's methods. This has absolutely no gain from the end user's perspective, it is simply a nice addition to help the developer. Consequently how do I tell the closure compiler to ignore the below line when it sees it?

goog.implements(myClass, fooInterface);

有可能吗?

推荐答案

这取决于您所忽略的含义.您是否希望将其编译为空,以便仅在未编译的代码中工作?如果是这样,则可以使用标准@define值之一:

It depends on what you mean by ignore. Do you want it to compile down to nothing so that it only works in uncompiled code? If so, you can use one of the standard @define values:

goog.implements = function (ctor, interfaceCtor) {
  if (!COMPILED) {
    ...
  }
};

或仅在启用goog.DEBUG的情况下:

or alternately, only when goog.DEBUG is enabled:

goog.implements = function (ctor, interfaceCtor) {
  if (goog.DEBUG) {
    ...
  }
};

如果这些都不适合,您可以定义自己的内容.

if these don't fit you can define your own.

或者您是完全不是其他意思吗?

Or do you mean something else entirely?

这篇关于如何告诉闭包编译器忽略代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆