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} How to document ECMA6 classes with JSDoc? */
const CONST_1 = "1";
/** @const {Number} How to document ECMA6 classes with JSDoc? */
const CONST_2 = 2;

//An example class
/** MyClass description */
class MyClass {

    //the class constructor
    /**
     * constructor description
     * @param  {[type]} config How to document ECMA6 classes with JSDoc?
     */
    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      How to document ECMA6 classes with JSDoc?
     * @param  {String} fruit.name How to document ECMA6 classes with JSDoc?
     * @return {String}            How to document ECMA6 classes with JSDoc?
     */
    methodTwo(fruit) {
        return "he likes " + fruit.name;
    }

    //private method
    /**   
     * methodThree description
     * @private
     * @param  {String} str How to document ECMA6 classes with JSDoc?
     * @return {String}     How to document ECMA6 classes with JSDoc?
     */
    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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)