Skip to content

Commit 32bb6ed

Browse files
committed
show how to document ES 2015 classes and modules (#110, #113)
1 parent 7999121 commit 32bb6ed

10 files changed

+490
-6
lines changed

content/en/howto-amd-modules.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: AMD Modules
33
description: How to add JSDoc comments to AMD and RequireJS modules.
44
related:
55
- about-namepaths.html
6+
- tags-exports.html
7+
- tags-module.html
68
---
79

810
## Overview

content/en/howto-commonjs-modules.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: CommonJS Modules
33
description: How to add JSDoc comments to CommonJS and Node.js modules.
44
related:
55
- about-namepaths.html
6+
- tags-exports.html
7+
- tags-module.html
68
---
79

810
## Overview

content/en/howto-es2015-classes.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
title: ES 2015 Classes
3+
description: How to add JSDoc comments to ECMAScript 2015 classes.
4+
related:
5+
- tags-augments.html
6+
---
7+
8+
JSDoc 3 makes it easy to document classes that follow the [ECMAScript 2015
9+
specification][es2015-classes]. You don't need to use tags such as `@class` and `@constructor` with
10+
ES 2015 classes—JSDoc automatically identifies classes and their constructors simply by parsing your
11+
code. ES 2015 classes are supported in JSDoc 3.4.0 and later.
12+
13+
[es2015-classes]: http://www.ecma-international.org/ecma-262/6.0/#sec-class-definitions
14+
15+
16+
## Documenting a simple class
17+
18+
The following example shows how to document a simple class with a constructor, two instance methods,
19+
and one static method:
20+
21+
{% example "Simple ES 2015 class" %}
22+
23+
```js
24+
/** Class representing a point. */
25+
class Point {
26+
/**
27+
* Create a point.
28+
* @param {number} x - The x value.
29+
* @param {number} y - The y value.
30+
*/
31+
constructor(x, y) {
32+
// ...
33+
}
34+
35+
/**
36+
* Get the x value.
37+
* @return {number} The x value.
38+
*/
39+
getX() {
40+
// ...
41+
}
42+
43+
/**
44+
* Get the y value.
45+
* @return {number} The y value.
46+
*/
47+
getY() {
48+
// ...
49+
}
50+
51+
/**
52+
* Convert a string containing two comma-separated numbers into a point.
53+
* @param {string} str - The string containing two comma-separated numbers.
54+
* @return {Point} A Point object.
55+
*/
56+
static fromString(str) {
57+
// ...
58+
}
59+
}
60+
```
61+
62+
{% endexample %}
63+
64+
You can also document classes that are defined in a class expression, which assigns the class to a
65+
variable or constant:
66+
67+
{% example "ES 2015 class expression" %}
68+
69+
```js
70+
/** Class representing a point. */
71+
const Point = class {
72+
// and so on
73+
}
74+
```
75+
76+
{% endexample %}
77+
78+
79+
## Extending classes
80+
81+
When you use the `extends` keyword to extend an existing class, you also need to tell JSDoc which
82+
class you're extending. You do this with the [`@augments` (or `@extends`) tag][augments-tag].
83+
84+
For example, to extend the `Point` class shown above:
85+
86+
{% example "Extending an ES 2015 class" %}
87+
88+
```js
89+
/**
90+
* Class representing a dot.
91+
* @extends Point
92+
*/
93+
class Dot extends Point {
94+
/**
95+
* Create a dot.
96+
* @param {number} x - The x value.
97+
* @param {number} y - The y value.
98+
* @param {number} width - The width of the dot, in pixels.
99+
*/
100+
constructor(x, y, width) {
101+
// ...
102+
}
103+
104+
/**
105+
* Get the dot's width.
106+
* @return {number} The dot's width, in pixels.
107+
*/
108+
getWidth() {
109+
// ...
110+
}
111+
}
112+
```
113+
114+
{% endexample %}
115+
116+
[augments-tag]: tags-augments.html

content/en/howto-es2015-modules.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: ES 2015 Modules
3+
description: How to add JSDoc comments to ECMAScript 2015 modules.
4+
related:
5+
- about-namepaths.html
6+
- tags-module.html
7+
---
8+
9+
JSDoc 3 makes it possible to document modules that follow the [ECMAScript 2015
10+
specification][es2015-modules]. ES 2015 modules are supported in JSDoc 3.4.0 and later.
11+
12+
13+
## Module identifiers
14+
15+
When you document an ES 2015 module, you'll use a [`@module` tag][module-tag] to document the identifier for the module. For example, if users load the module by calling `import * as myShirt
16+
from 'my/shirt'`, you'll write a JSDoc comment that contains the tag `@module my/shirt`.
17+
18+
If you use the `@module` tag without a value, JSDoc will try to guess the correct module identifier
19+
based on the filepath.
20+
21+
When you use a JSDoc [namepath][namepaths] to refer to a module from another JSDoc comment, you must
22+
add the prefix `module:`. For example, if you want the documentation for the module `my/pants` to
23+
link to the module `my/shirt`, you could use the [`@see` tag][see-tag] to document `my/pants` as
24+
follows:
25+
26+
```js
27+
/**
28+
* Pants module.
29+
* @module my/pants
30+
* @see module:my/shirt
31+
*/
32+
```
33+
34+
Similarly, the namepath for each member of the module will start with `module:`, followed by the
35+
module name. For example, if your `my/pants` module exports a `Jeans` class, and `Jeans` has an
36+
instance method named `hem`, the instance method's longname is `module:my/pants.Jeans#hem`.
37+
38+
[module-tag]: tags-module.html
39+
[namepaths]: about-namepaths.html
40+
[see-tag]: tags-see.html
41+
42+
43+
## Exported values
44+
45+
The following example shows how to document different kinds of exported values in an ES 2015 module.
46+
In most cases, you can simply add a JSDoc comment to the `export` statement that defines the
47+
exported value. If you are exporting a value under another name, you can document the exported value
48+
within its `export` block.
49+
50+
{% example "Documenting values exported by a module" %}
51+
52+
```js
53+
/** @module color/mixer */
54+
55+
/** The name of the module. */
56+
export const name = 'mixer';
57+
58+
/** The most recent blended color. */
59+
export var lastColor = null;
60+
61+
/**
62+
* Blend two colors together.
63+
* @param {string} color1 - The first color, in hexidecimal format.
64+
* @param {string} color2 - The second color, in hexidecimal format.
65+
* @return {string} The blended color.
66+
*/
67+
export function blend(color1, color2) {}
68+
69+
// convert color to array of RGB values (0-255)
70+
function rgbify(color) {}
71+
72+
export {
73+
/**
74+
* Get the red, green, and blue values of a color.
75+
* @function
76+
* @param {string} color - A color, in hexidecimal format.
77+
* @returns {Array.<number>} An array of the red, green, and blue values,
78+
* each ranging from 0 to 255.
79+
*/
80+
rgbify as toRgb
81+
}
82+
```
83+
84+
{% endexample %}
85+
86+
[es2015-modules]: http://www.ecma-international.org/ecma-262/6.0/#sec-modules

data/toc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
}
3939
],
4040
"examples": [
41+
{
42+
"filename": "howto-es2015-classes.html"
43+
},
44+
{
45+
"filename": "howto-es2015-modules.html"
46+
},
4147
{
4248
"filename": "howto-commonjs-modules.html"
4349
},

howto-amd-modules.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,17 @@ <h2 id="multiple-modules-defined-in-one-file">Multiple modules defined in one fi
201201
</code></pre>
202202
</figure>
203203
<h2 id="related-links">Related Links</h2>
204-
<p>
205-
<a href="about-namepaths.html">Using namepaths with JSDoc 3</a>
206-
</p>
204+
<ul>
205+
<li>
206+
<a href="about-namepaths.html">Using namepaths with JSDoc 3</a>
207+
</li>
208+
<li>
209+
<a href="tags-exports.html">@exports</a>
210+
</li>
211+
<li>
212+
<a href="tags-module.html">@module</a>
213+
</li>
214+
</ul>
207215
</article>
208216
<footer>
209217
<a class="license-badge" href="http://creativecommons.org/licenses/by-sa/3.0/">

howto-commonjs-modules.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,17 @@ <h2 id="properties-added-to-this-">Properties added to &#39;this&#39;</h2>
276276
</code></pre>
277277
</figure>
278278
<h2 id="related-links">Related Links</h2>
279-
<p>
280-
<a href="about-namepaths.html">Using namepaths with JSDoc 3</a>
281-
</p>
279+
<ul>
280+
<li>
281+
<a href="about-namepaths.html">Using namepaths with JSDoc 3</a>
282+
</li>
283+
<li>
284+
<a href="tags-exports.html">@exports</a>
285+
</li>
286+
<li>
287+
<a href="tags-module.html">@module</a>
288+
</li>
289+
</ul>
282290
</article>
283291
<footer>
284292
<a class="license-badge" href="http://creativecommons.org/licenses/by-sa/3.0/">

0 commit comments

Comments
 (0)