加载中...

ES 2015 Classes(类)


JSDoc3 描述一个遵循 ECMAScript 2015规范的类是很简单的。你并不需要使用诸如如@class@constructor的标签来描述 ES 2015 classes,JSDoc通过分析你的代码会自动识别类和它们的构造函数。ES 2015 classes在JSDoc3.4.0及更高版本支持。

Documenting a simple class(文档化一个简单的类)

下面的例子演示了如何通过一个构造函数,两个实例方法和一个静态方法文档化一个简单的类:

例如,简单的 ES 2015 类 :

  1. /** Class representing a point. */
  2. class Point {
  3. /**
  4. * Create a point.
  5. * @param {number} x - The x value.
  6. * @param {number} y - The y value.
  7. */
  8. constructor(x, y) {
  9. // ...
  10. }
  11. /**
  12. * Get the x value.
  13. * @return {number} The x value.
  14. */
  15. getX() {
  16. // ...
  17. }
  18. /**
  19. * Get the y value.
  20. * @return {number} The y value.
  21. */
  22. getY() {
  23. // ...
  24. }
  25. /**
  26. * Convert a string containing two comma-separated numbers into a point.
  27. * @param {string} str - The string containing two comma-separated numbers.
  28. * @return {Point} A Point object.
  29. */
  30. static fromString(str) {
  31. // ...
  32. }
  33. }

您还可以记录类表达式中定义的类,将其分配给一个变量或常量:

例如,ES 2015 类表达式:

  1. /** Class representing a point. */
  2. const Point = class {
  3. // and so on
  4. }

Extending classes(扩展类)

当您使用 extends关键字来扩展一个现有的类的时候,你还需要告诉JSDoc哪个类是你要扩展的。 为此,您可以使用 @augments (或 @extends) 标签。

例如,扩展如上所示Point 类,扩展一个 ES 2015 类:

  1. /**
  2. * Class representing a dot.
  3. * @extends Point
  4. */
  5. class Dot extends Point {
  6. /**
  7. * Create a dot.
  8. * @param {number} x - The x value.
  9. * @param {number} y - The y value.
  10. * @param {number} width - The width of the dot, in pixels.
  11. */
  12. constructor(x, y, width) {
  13. // ...
  14. }
  15. /**
  16. * Get the dot's width.
  17. * @return {number} The dot's width, in pixels.
  18. */
  19. getWidth() {
  20. // ...
  21. }
  22. }

还没有评论.