How to document ECMA6 classes with JSDoc?
Late answer, but since I came across this googling something else I thought I'd have a crack at it.
You've probably found by now that the JSDoc site has decent explanations and examples on how to document ES6 features.
Given that, here's how I would document your example:
/**
* module description
* @module MyClass
*/
//constants to be documented.
//I usually use the @const, @readonly and @default tags for them
/** @const {String} [description] */
const CONST_1 = "1";
/** @const {Number} [description] */
const CONST_2 = 2;
//An example class
/** MyClass description */
class MyClass {
//the class constructor
/**
* constructor description
* @param {[type]} config [description]
*/
constructor(config) {
//class members. Should be private.
/** @private */
this.member1 = config;
/** @private */
this.member2 = "bananas";
}
//A normal method, public
/** methodOne description */
methodOne() {
console.log( methodThree("I like bananas"));
}
//Another method. Receives a Fruit object parameter, public
/**
* methodTwo description
* @param {Object} fruit [description]
* @param {String} fruit.name [description]
* @return {String} [description]
*/
methodTwo(fruit) {
return "he likes " + fruit.name;
}
//private method
/**
* methodThree description
* @private
* @param {String} str [description]
* @return {String} [description]
*/
methodThree(str) {
return "I think " + str;
}
}
module.exports = MyClass;
Note that @const implies readonly and default automatically. JSDoc will pick up the export, the @class and the @constructor correctly, so only the oddities like private members need to be specified.
For anyone visiting this question in 2019:
The answer given by @FredStark is still correct, however, the following points should be noted:
- Most/all of the links in this page are dead. For the documentation of the JSDoc, you can see here.
- In many IDEs or code editors (such as VSCode), you will find auto-completions such as
@class
or@constructor
which are not necessary for the case of ES6 classes since these editors will recognize the constructor after thenew
keyword.