Sub topic 6
Documenting Classes and Modules
This chapter assumes you can write ES classes and use import and export. It also assumes you already know @param, @returns, and basic JSDoc type syntax from earlier chapters. Here you will apply those tags to classes, their members, and whole modules.
Where Class Comments Go
A class has several places that can carry a JSDoc block. Each one documents a different thing:
| Location | What it documents | Typical tags |
|---|---|---|
| Above `class` | The class's purpose | description, `@example` |
| Above `constructor` | Construction arguments | `@param` |
| Above `this.x = ...` | An instance property | `@type`, `@readonly` |
| Above a method | One behavior | `@param`, `@returns`, `@throws` |
| Above `static` or `get` | Static members and accessors | `@returns`, `@type` |
Documenting a Class
Here is a small class with each part documented:
/**
* A simple in-memory counter.
*/
export class Counter {
/**
* @param {number} [start=0] - Initial value.
*/
constructor(start = 0) {
/** @type {number} */
this.value = start;
}
/**
* Increases the counter.
* @param {number} [step=1] - Amount to add.
* @returns {number} The new value.
*/
increment(step = 1) {
this.value += step;
return this.value;
}
}Constructor parameters belong on the constructor, not on the class. Tools such as TypeScript and VS Code read them from there. The class comment should say what the class is for.
Instance properties are created by assignment, so the comment goes directly above the assignment. A one-line /** @type {number} */ is enough when the name explains itself.
Static Members, Getters, and Visibility
Static methods and accessors use the same tags as ordinary methods. A getter documents what it returns. JSDoc also has tags that describe how a member is meant to be used:
/**
* Tracks elapsed time between start and stop calls.
*/
export class Timer {
/**
* Creates a timer that has already started.
* @returns {Timer}
*/
static started() {
const timer = new Timer();
timer.start();
return timer;
}
constructor() {
/**
* Start time in milliseconds, or null if not started.
* @type {number | null}
* @private
*/
this._startedAt = null;
/**
* Unit used when reporting elapsed time.
* @type {string}
* @readonly
*/
this.unit = 'ms';
}
/** Records the current time as the start. */
start() {
this._startedAt = Date.now();
}
/**
* Milliseconds since start, or 0 if not started.
* @returns {number}
*/
get elapsed() {
return this._startedAt === null ? 0 : Date.now() - this._startedAt;
}
}
const t = Timer.started();
console.log(t.elapsed >= 0, t.unit); // true 'ms'@private hides the member from generated docs. TypeScript's checker also reports access to it from outside the class. @readonly marks a property that callers should not reassign. Both tags only state intent. JavaScript still lets anyone write to these properties at runtime. If you need real privacy, use #private fields. JSDoc and TypeScript understand those without any tag.
Documenting a Module
A module is one file. To describe the whole file, put a block at the top with @module or @file:
/**
* Helpers for formatting money values.
* @module money
*/
/**
* Formats a number of cents as a currency string.
* @param {number} cents - Amount in cents.
* @param {string} [currency='USD'] - ISO 4217 currency code.
* @returns {string} The formatted amount, for example "$12.50".
*/
export function formatCents(cents, currency = 'USD') {
return new Intl.NumberFormat('en-US', { style: 'currency', currency })
.format(cents / 100);
}The jsdoc generator uses @module money to group every export from this file under one "money" page. TypeScript ignores @module, so the tag only affects the generated site. Use @file (or its alias @fileoverview) when you want to describe the file without naming a module.
Common Errors
- Putting
@paramon the class comment. Editors won't connect those parameters tonew Counter(...). Move them to the constructor. - Leaving a blank line or code between a comment and its target. A JSDoc block attaches to the declaration that follows it. If other code sits in between, the comment documents nothing useful.
- Using
//comments for properties. Only/** */blocks count as JSDoc.// the valueproduces no type information. - Trusting
@privatefor safety. The tag documents intent but doesn't enforce anything at runtime.
Exercise
A class has /** @param {string} name */ above the class keyword and no comment on its constructor. Hovering over new Person('Ada') in VS Code shows no parameter type. What should you change?
Show answer
Move the @param {string} name tag into a JSDoc block directly above the constructor. Constructor parameters are read from the constructor's comment, and the class comment should describe what the class is for.
Exercise
You want a property id that callers shouldn't reassign, and you want generated docs to show it. Which tags go in its comment, and does this stop reassignment at runtime?
Show answer
Use /** @type {string} @readonly */ above this.id = ... and leave out @private so the property still appears in the docs. It doesn't stop reassignment at runtime. Type checkers report an error, but plain JavaScript still allows the write.