Skip to content

Commit b17504a

Browse files
committed
explain how multiple inheritance works in >=3.3.0 (#66)
1 parent bbe713f commit b17504a

File tree

3 files changed

+125
-34
lines changed

3 files changed

+125
-34
lines changed

content/en/tags-augments.md

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
tag: augments
3-
description: This object adds onto a parent object.
3+
description: Indicate that a symbol inherits from, ands adds to, a parent symbol.
44
synonyms:
55
- extends
66
related:
@@ -17,22 +17,21 @@ related:
1717

1818
## Overview
1919

20-
The @augments or @extends tag marks a symbol as augmenting another symbol.
20+
The `@augments` or` @extends` tag indicates that a symbol inherits from, and potentially adds to, a
21+
parent symbol. You can use this tag to document both class-based and prototype-based inheritance.
2122

22-
While current versions of JavaScript don't allow classes or subclasses in the same way that
23-
class-based languages like Java do, many programmers choose to think of their code structure in
24-
these terms. For this purpose JSDoc provides the @class and @extends tags. If you wish to express a
25-
similar relationship between two symbols, but don't wish to promote the classical analogy, you can
26-
use the @constructor and @augments tags instead.
23+
In JSDoc 3.3.0 and later, if a symbol inherits from multiple parents, and both parents have
24+
identically named members, JSDoc uses the documentation from the last parent that is listed in the
25+
JSDoc comment.
2726

2827

2928
## Examples
3029

31-
In the example below I wish to document the fact that Ducks are a specialised form of Animal:
32-
meaning that Duck instances are like Animal instances which have been augmented with additional
33-
properties.
30+
In the following example, the `Duck` class is defined as a subclass of `Animal`. `Duck` instances
31+
have the same properties as `Animal` instances, as well as a `speak` method that is unique to `Duck`
32+
instances.
3433

35-
{% example "Documenting a class/subclass type of relationship." %}
34+
{% example "Documenting a class/subclass relationship" %}
3635

3736
```js
3837
/**
@@ -47,23 +46,71 @@ function Animal() {
4746
* @constructor
4847
* @augments Animal
4948
*/
50-
function Duck() {
51-
}
49+
function Duck() {}
5250
Duck.prototype = new Animal();
5351

5452
/** What do ducks say? */
5553
Duck.prototype.speak = function() {
5654
if (this.alive) {
5755
alert('Quack!');
5856
}
59-
}
57+
};
6058

6159
var d = new Duck();
6260
d.speak(); // Quack!
63-
d.alive = false; // dead duck?
64-
d.speak();
61+
d.alive = false;
62+
d.speak(); // (nothing)
6563
```
6664
{% endexample %}
6765

68-
A related pattern can be documented with the @mixin and @mixes tags. Or, if the augmented symbol is
69-
only reusing one or two members of another symbol, you may find the @borrows tag more convenient.
66+
In the following example, the `Duck` class inherits from both the `Flyable` and `Bird` classes, both
67+
of which define a `takeOff` method. Because the documentation for `Duck` lists `@augments Bird`
68+
last, JSDoc automatically documents `Duck#takeOff` using the comment from `Bird#takeOff`.
69+
70+
{% example "Multiple inheritance with duplicated method names" %}
71+
72+
```js
73+
/**
74+
* Abstract class for things that can fly.
75+
* @class
76+
*/
77+
function Flyable() {
78+
this.canFly = true;
79+
}
80+
81+
/** Take off. */
82+
Flyable.prototype.takeOff = function() {
83+
// ...
84+
};
85+
86+
/**
87+
* Abstract class representing a bird.
88+
* @class
89+
*/
90+
function Bird(canFly) {
91+
this.canFly = canFly;
92+
}
93+
94+
/** Spread your wings and fly, if possible. */
95+
Bird.prototype.takeOff = function() {
96+
if (this.canFly) {
97+
this._spreadWings()
98+
._run()
99+
._flapWings();
100+
}
101+
};
102+
103+
/**
104+
* Class representing a duck.
105+
* @class
106+
* @augments Flyable
107+
* @augments Bird
108+
*/
109+
function Duck() {}
110+
111+
// Described in the docs as "Spread your wings and fly, if possible."
112+
Duck.prototype.takeOff = function() {
113+
// ...
114+
};
115+
```
116+
{% endexample %}

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ <h2 id="tag-dictionary">Tag Dictionary</h2>
6868
<dt><a href="tags-alias.html">@alias</a></dt>
6969
<dd>Treat a member as if it had a different name.</dd>
7070
<dt><a href="tags-augments.html">@augments</a> (synonyms: @extends)</dt>
71-
<dd>This object adds onto a parent object.</dd>
71+
<dd>Indicate that a symbol inherits from, ands adds to, a parent symbol.</dd>
7272
<dt><a href="tags-author.html">@author</a></dt>
7373
<dd>Identify the author of an item.</dd>
7474
<dt><a href="tags-borrows.html">@borrows</a></dt>

tags-augments.html

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<head>
66
<meta charset="utf-8">
7-
<meta name="description" content="This object adds onto a parent object.">
7+
<meta name="description" content="Indicate that a symbol inherits from, ands adds to, a parent symbol.">
88
<title>Use JSDoc: @augments</title>
99
<link rel="stylesheet" href="styles/usejsdoc.css">
1010
<link rel="stylesheet" href="styles/prettify.css">
@@ -48,16 +48,16 @@ <h2 id="syntax">Syntax</h2>
4848
<p><code>@augments &lt;namepath&gt;</code>
4949
</p>
5050
<h2 id="overview">Overview</h2>
51-
<p>The @augments or @extends tag marks a symbol as augmenting another symbol.</p>
52-
<p>While current versions of JavaScript don&#39;t allow classes or subclasses in the same way that class-based languages like Java do, many programmers choose to
53-
think of their code structure in these terms. For this purpose JSDoc provides the @class and @extends tags. If you wish to express a similar relationship between
54-
two symbols, but don&#39;t wish to promote the classical analogy, you can use the @constructor and @augments tags instead.</p>
51+
<p>The <code>@augments</code> or<code>@extends</code> tag indicates that a symbol inherits from, and potentially adds to, a parent symbol. You can use this tag
52+
to document both class-based and prototype-based inheritance.</p>
53+
<p>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
54+
last parent that is listed in the JSDoc comment.</p>
5555
<h2 id="examples">Examples</h2>
56-
<p>In the example below I wish to document the fact that Ducks are a specialised form of Animal: meaning that Duck instances are like Animal instances which have
57-
been augmented with additional properties.
56+
<p>In the following example, the <code>Duck</code> class is defined as a subclass of <code>Animal</code>. <code>Duck</code> instances have the same properties as
57+
<code>Animal</code> instances, as well as a <code>speak</code> method that is unique to <code>Duck</code> instances.
5858
</p>
5959
<figure>
60-
<figcaption>Documenting a class/subclass type of relationship.</figcaption><pre class="prettyprint lang-js"><code>/**
60+
<figcaption>Documenting a class/subclass relationship</figcaption><pre class="prettyprint lang-js"><code>/**
6161
* @constructor
6262
*/
6363
function Animal() {
@@ -69,25 +69,69 @@ <h2 id="examples">Examples</h2>
6969
* @constructor
7070
* @augments Animal
7171
*/
72-
function Duck() {
73-
}
72+
function Duck() {}
7473
Duck.prototype = new Animal();
7574

7675
/** What do ducks say? */
7776
Duck.prototype.speak = function() {
7877
if (this.alive) {
7978
alert('Quack!');
8079
}
81-
}
80+
};
8281

8382
var d = new Duck();
8483
d.speak(); // Quack!
85-
d.alive = false; // dead duck?
86-
d.speak();
84+
d.alive = false;
85+
d.speak(); // (nothing)
86+
</code></pre>
87+
</figure>
88+
<p>In the following example, the <code>Duck</code> class inherits from both the <code>Flyable</code> and <code>Bird</code> classes, both of which define a <code>takeOff</code> method. Because the documentation for <code>Duck</code> lists <code>@augments Bird</code> last, JSDoc automatically documents <code>Duck#takeOff</code> using
89+
the comment from <code>Bird#takeOff</code>.</p>
90+
<figure>
91+
<figcaption>Multiple inheritance with duplicated method names</figcaption><pre class="prettyprint lang-js"><code>/**
92+
* Abstract class for things that can fly.
93+
* @class
94+
*/
95+
function Flyable() {
96+
this.canFly = true;
97+
}
98+
99+
/** Take off. */
100+
Flyable.prototype.takeOff = function() {
101+
// ...
102+
};
103+
104+
/**
105+
* Abstract class representing a bird.
106+
* @class
107+
*/
108+
function Bird(canFly) {
109+
this.canFly = canFly;
110+
}
111+
112+
/** Spread your wings and fly, if possible. */
113+
Bird.prototype.takeOff = function() {
114+
if (this.canFly) {
115+
this._spreadWings()
116+
._run()
117+
._flapWings();
118+
}
119+
};
120+
121+
/**
122+
* Class representing a duck.
123+
* @class
124+
* @augments Flyable
125+
* @augments Bird
126+
*/
127+
function Duck() {}
128+
129+
// Described in the docs as "Spread your wings and fly, if possible."
130+
Duck.prototype.takeOff = function() {
131+
// ...
132+
};
87133
</code></pre>
88134
</figure>
89-
<p>A related pattern can be documented with the @mixin and @mixes tags. Or, if the augmented symbol is only reusing one or two members of another symbol, you may
90-
find the @borrows tag more convenient.</p>
91135
<h2 id="related-links">Related Links</h2>
92136
<ul>
93137
<li>

0 commit comments

Comments
 (0)