如何在 javascript 中扩展抽象 class 并为闭包编译器注释但没有闭包库?

How do you extend an abstract class in javascript and annotate for closure compiler but without closure library?

假设我有一个摘要class

/**@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.

我想扩展这个 class,通常,我会做类似

的事情
/**@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 一直向上 class 链的能力,同时让编译器满意的方式来做到这一点?

我在这种情况下使用goog.inherits。由于您不想使用闭包库,您可以只从 goog.inherits 复制 closure-library/closure/goog/base.js。也许给它起个名字 googInherits。代码是这样的:

/**@constructor
 * @extends {AbsFoo}
 */
Foo = function(){
    AbsFoo.apply(this);
}
googInherits(Foo, AbsFoo);

/** @inheritDoc */
Foo.prototype.cMinusD = function(c,d){
    return c - d;
};