@use JSDoc

EC 2015 クラス

JSDoc 3 では、ECMAScript 2015 仕様 に従うクラスを簡単にドキュメント化できます。EC 2015 クラスでは、@class@constructor などのタグを使用する必要はありません。JSDoc は単にコードを解析するだけで、クラスとそのコンストラクタを自動的に識別します。EC 2015 クラスは JSDoc 3.4.0 以降でサポートされています。

単純なクラスのドキュメント化

次の例では、コンストラクタ、2 つのインスタンスメソッド、1 つの静的メソッドを持つ単純なクラスをドキュメント化する手順を示します。

EC 2015 の単純なクラス
/** Class representing a point. */
class Point {
    /**
     * Create a point.
     * @param {number} x - The x value.
     * @param {number} y - The y value.
     */
    constructor(x, y) {
        // ...
    }

    /**
     * Get the x value.
     * @return {number} The x value.
     */
    getX() {
        // ...
    }

    /**
     * Get the y value.
     * @return {number} The y value.
     */
    getY() {
        // ...
    }

    /**
     * Convert a string containing two comma-separated numbers into a point.
     * @param {string} str - The string containing two comma-separated numbers.
     * @return {Point} A Point object.
     */
    static fromString(str) {
        // ...
    }
}

クラスをクラス式で定義して、変数や定数にクラスを割り当てることもドキュメント化できます。

EC 2015 クラス式
/** Class representing a point. */
const Point = class {
    // and so on
}

クラスの拡張

extends キーワードを使用して既存のクラスを拡張する場合、JSDoc に拡張するクラスも伝える必要があります。これには、@augments (または @extends) タグ を使用します。

たとえば、前に示した Point クラスを拡張する場合

EC 2015 クラスの拡張
/**
 * Class representing a dot.
 * @extends Point
 */
class Dot extends Point {
    /**
     * Create a dot.
     * @param {number} x - The x value.
     * @param {number} y - The y value.
     * @param {number} width - The width of the dot, in pixels.
     */
    constructor(x, y, width) {
        // ...
    }

    /**
     * Get the dot's width.
     * @return {number} The dot's width, in pixels.
     */
    getWidth() {
        // ...
    }
}