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:
/** 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:
/** Class representing a point. */
const Point = class {
// and so on
}
扩展课程
¥Extending classes
当你使用 extends
关键字扩展现有类时,你还需要告诉 JSDoc 你要扩展哪个类。你可以使用 @augments
(或 @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:
/**
* 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() {
// ...
}
}