我决定我需要的东西来帮助我一点,而实现接口。 所以我添加了这个功能,在封闭库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);
是有可能吗?