[英]How to annotate in javascript a class that extends a generic type, using closure compiler
[英]How do you extend an abstract class in javascript and annotate for closure compiler but without closure library?
說我有一個抽象課
/**@constructor
* @abstract*/
function AbsFoo(){}
/**@return {number}
* @param {number} a
* @param {number} b */
AbsFoo.prototype.aPlusB = function(a,b){
return a + b
};
/**@abstract
* @return {number}
* @param {number} c
* @param {number} d */
AbsFoo.prototype.cMinusD = function(c,d){}; //extending class need to implement.
我想擴展此類,通常,我會做類似的事情
/**@constructor
* @extends {AbsFoo} */
function Foo(){
AbsFoo.apply(this);
}
Foo.prototype = new AbsFoo();
Foo.prototype.constructor = Foo;
Foo.prototype.doSomething = function(c,d){
return c - d;
};
但是閉包編譯器說
JSC_INSTANTIATE_ABSTRACT_CLASS: cannot instantiate abstract class
參考行Foo.prototype = new AbsFoo();
那么,我該如何保持原型的繼承性以及在類鏈中始終使用instanceof
的能力,同時又使編譯器感到滿意呢?
在這種情況下,我使用goog.inherits
。 由於您不想使用閉包庫,因此可以僅從closure-library / closure / goog / base.js復制goog.inherits
。 也許給它起名字googInherits
。 代碼如下所示:
/**@constructor
* @extends {AbsFoo}
*/
Foo = function(){
AbsFoo.apply(this);
}
googInherits(Foo, AbsFoo);
/** @inheritDoc */
Foo.prototype.cMinusD = function(c,d){
return c - d;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.