Synonyms
extends
语法
🌐 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 实例相同的属性,以及 speak 方法,该方法是 Duck 实例独有的。
🌐 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 类同时继承自 Flyable 和 Bird 类,这两个类都定义了一个 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() {
// ...
};