Javascript code comment specification

1. Grammar

1.1 Explanation of notes

Write in the first line of the comment block.

/**
 * events-function(This is the description of the comment)
 * @description Switching audio playback status (play/stop)
 */
togglePlay: function() {
    // Omit other codes...
}

1.2 Tags

@tagName

/** @function */
function fn() {

}

1.3 Description of the label

/**
 * @constructor Student - Students
 * @param {string} name - Student's name
 */
 function Student(name) {
     
 }

1.4 Type

/**
 * @param {string} a Must pass parameters
 */
function fn(a, b, c) {

}

1.5 Optional parameters

/**
 *  @param {string} a 
 *  @param {number} [b] Optional parameters
 */
function fn(a, b) {

}

1.6 Parameters have default values

/**
 *  @param {string} a Must pass parameters
 *  @param {number} [c=666] Parameters have default values
 */
function fn(a, c) {

}

1.7 Link

/**
 * See {@link MyClass} and [MyClass's foo property]{@link MyClass#foo}.
 * Also, check out {@link http://www.google.com|Google} and
 * {@link https://github.com GitHub}.
 */

2. Example

2.1 Function

/**
 * Convert time strings to time objects
 * @function _str2time
 * @param strTime {String} - e.g "2017-02-13 10:02:58" or "2017-02-13" or "9:10"
 * @param type {String} - e.g date, dateTime, time
 */
function _str2time(strTime, type) {
    // Omit other codes
}

2.2 Classes/Constructor

/**
 * Timer
 * @class Timer
 */

function Timer() {
    this._timeId = 0;
    this._eventId = -1;

    this.eventHandler = {
        'stop': {}
    };

    /**
     * Whether the timer is in the stop state
     * @memberof Timer
     * @member stopped
     * @instance
     */
    this.stopped = true;

    /**
     * Start Timer
     * @memberof Timer
     * @instance
     * @method start
     * @param {function} handler - Functions to be called on each execution of the timer
     * @param {number} interval - Timer execution time interval
     */
    this.start = function (handler, interval) {
        this.stopped = false;
        let _recursion = function() {
            this._timeId = setTimeout(() => {
                handler()
                    .then(() => {
                        if (this.stopped) {
                            clearTimeout(this._timeId);
                            this._trigger('stop');
                            return;
                        }
                        _recursion();
                    })
                    .catch(err => {
                        clearTimeout(this._timeId);
                        this.stopped = true;
                        this._trigger('stop');
                        if (err) throw new Error(err);
                    });
            }, interval)
        }.bind(this);
        _recursion();
    }

    /**
     * Stop Timer
     * @memberof Timer
     * @instance
     * @method stop
     */
    this.stop = function () {
        this.stopped = true;
    }
}

/**
 * Listening to events
 * @memberof Timer
 * @instance
 * @method on
 * @param {string} type - Event Type  e.g 'stop'
 * @param {function} fn - Event handling functions
 * @return {number} eventId - Event handler Id for unlistening
 */
Timer.prototype.on = function (type, fn) {
    let _eventId = fn.name || ++this._eventId;
    this.eventHandler[type][_eventId] = fn;
    return _eventId;
}

/**
 * Trigger events
 * @private
 */
Timer.prototype._trigger = function (type) {
    let handlerMap = this.eventHandler[type];
    for (let key in handlerMap) {
        handlerMap[key]();
    }
}

/**
 * Cancel listening to events
 * @memberof Timer
 * @instance
 * @method off
 * @param {string} type - Event Type  e.g 'stop'
 * @param {function} target - Event handling function Id or function name
 */
Timer.prototype.off = function (type, target) {
    let _target = (typeof target === 'function') ? target.name : target;
    delete this.eventHandler[type][_target];
}

Leave a Reply