JSDoc 中文网

Synonyms

语法

¥Syntax

@augments <namepath>

概述

¥Overview

@augments @extends 标签指示符号继承自父符号,并可能添加到父符号。你可以使用此标记来记录基于类和基于原型的继承。

¥The @augments or @extends tag indicates that a symbol inherits from, and potentially adds to, a parent symbol. You can use this tag to document both class-based and prototype-based inheritance.

在 JSDoc 3.3.0 及更高版本中,如果一个符号继承自多个父级,并且两个父级具有相同名称的成员,则 JSDoc 将使用 JSDoc 注释中列出的最后一个父级的文档。

¥In JSDoc 3.3.0 and later, if a symbol inherits from multiple parents, and both parents have identically named members, JSDoc uses the documentation from the last parent that is listed in the JSDoc comment.

示例

¥Examples

在以下示例中,Duck 类被定义为 Animal 的子类。Duck 实例具有与 Animal 实例相同的属性,以及 Duck 实例特有的 speak 方法。

¥In the following example, the Duck class is defined as a subclass of Animal. Duck instances have the same properties as Animal instances, as well as a speak method that is unique to Duck instances.

记录类/子类关系
/**

 * @constructor
 */
function Animal() {
    /** Is this animal alive? */
    this.alive = true;
}

/**

 * @constructor

 * @augments Animal
 */
function Duck() {}
Duck.prototype = new Animal();

/** What do ducks say? */
Duck.prototype.speak = function() {
    if (this.alive) {
        alert('Quack!');
    }
};

var d = new Duck();
d.speak(); // Quack!
d.alive = false;
d.speak(); // (nothing)

在以下示例中,Duck 类继承自 FlyableBird 类,这两个类都定义了 takeOff 方法。因为 Duck 的文档最后列出了 @augments Bird,所以 JSDoc 使用 Bird#takeOff 中的注释自动记录 Duck#takeOff

¥In the following example, the Duck class inherits from both the Flyable and Bird classes, both of which define a takeOff method. Because the documentation for Duck lists @augments Bird last, JSDoc automatically documents Duck#takeOff using the comment from Bird#takeOff.

具有重复方法名称的多重继承
/**

 * Abstract class for things that can fly.

 * @class
 */
function Flyable() {
    this.canFly = true;
}

/** Take off. */
Flyable.prototype.takeOff = function() {
    // ...
};

/**

 * Abstract class representing a bird.

 * @class
 */
function Bird(canFly) {
    this.canFly = canFly;
}

/** Spread your wings and fly, if possible. */
Bird.prototype.takeOff = function() {
    if (this.canFly) {
        this._spreadWings()
            ._run()
            ._flapWings();
    }
};

/**

 * Class representing a duck.

 * @class

 * @augments Flyable

 * @augments Bird
 */
function Duck() {}

// Described in the docs as "Spread your wings and fly, if possible."
Duck.prototype.takeOff = function() {
    // ...
};