我决定在实现接口时需要一些帮助。所以我在闭包库的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);
有可能吗?