ES2015 Classes
Overview
JSDoc automatically recognises ES2015 class declarations — you don't need @class, @constructor, or @augments for standard class syntax. Just write your JSDoc comments and JSDoc handles the rest.
Generate docs from your classes
npx jsdoc -r src -d docsSimple class
/** Represents a 2D point. */
class Point {
/**
* Create a point.
* @param {number} x - The x coordinate.
* @param {number} y - The y coordinate.
*/
constructor(x, y) {
this.x = x;
this.y = y;
}
/**
* Calculate the distance to another point.
* @param {Point} other
* @returns {number}
*/
distanceTo(other) {
return Math.hypot(this.x - other.x, this.y - other.y);
}
/**
* Create a Point from an `[x, y]` array.
* @param {number[]} arr
* @returns {Point}
*/
static fromArray([x, y]) {
return new Point(x, y);
}
}
Inheritance
JSDoc detects extends automatically:
/**
* A point in 3D space.
*/
class Point3D extends Point {
/**
* @param {number} x
* @param {number} y
* @param {number} z - The z coordinate.
*/
constructor(x, y, z) {
super(x, y);
this.z = z;
}
}
You can add @augments Point for an explicit link in the documentation:
/**
* @augments Point
*/
class Point3D extends Point {}
Abstract methods
Use @abstract on methods that subclasses must override:
/** An abstract geometric shape. */
class Shape {
/**
* @abstract
* @returns {number}
*/
area() {
throw new Error('area() must be implemented by subclass.');
}
}
Interfaces and mixins
/**
* @interface
*/
class Serializable {
/** @returns {string} */
toJSON() {}
}
/**
* @implements {Serializable}
*/
class Product {
toJSON() { return JSON.stringify(this); }
}
Private members
class BankAccount {
#balance = 0;
/**
* Deposit funds.
* @param {number} amount
*/
deposit(amount) {
this.#balance += amount;
}
/**
* @private
*/
_log(msg) {
console.log(msg);
}
}
See also
Official reference: jsdoc.app/howto-es2015-classes