JSDoc 中文网

ES 2015 课程

JSDoc 3 使得为遵循 ECMAScript 2015 规范 的类编写文档变得容易。对于 ES 2015 类,你不需要使用诸如 @class@constructor 的标签——JSDoc 只需解析你的代码就能自动识别类及其构造函数。JSDoc 3.4.0 及更高版本支持 ES 2015 类。

🌐 JSDoc 3 makes it easy to document classes that follow the ECMAScript 2015 specification. You don't need to use tags such as @class and @constructor with ES 2015 classes—JSDoc automatically identifies classes and their constructors simply by parsing your code. ES 2015 classes are supported in JSDoc 3.4.0 and later.

记录一个简单的类

🌐 Documenting a simple class

下面的例子展示了如何记录一个包含构造函数、两个实例方法和一个静态方法的简单类:

🌐 The following example shows how to document a simple class with a constructor, two instance methods, and one static method:

简单的 ES 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) {
        // ...
    }
}

你也可以为在类表达式中定义的类编写文档,该表达式将类分配给一个变量或常量:

🌐 You can also document classes that are defined in a class expression, which assigns the class to a variable or constant:

ES 2015 类表达式
/** Class representing a point. */
const Point = class {
    // and so on
}

扩展课程

🌐 Extending classes

当你使用 extends 关键字来扩展一个现有类时,你还需要告诉 JSDoc 你正在扩展哪个类。你可以使用 @extends(或 @extends)标签 来完成此操作。

🌐 When you use the extends keyword to extend an existing class, you also need to tell JSDoc which class you're extending. You do this with the @augments (or @extends) tag.

例如,要扩展上面显示的 Point 类:

🌐 For example, to extend the Point class shown above:

扩展 ES 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() {
        // ...
    }
}