Javascript 註釋規範

Javascript 代碼註釋規範

1、語法

1. 註釋的說明

語法:寫在註釋塊第一行javascript

/**
 * events-function(這是註釋的說明)
 * @description 切換音頻播放狀態(播放/中止)
 */
togglePlay: function() {
    // 省略其它代碼...
}

2. 標籤

語法:@tagNamejava

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

}

3. 標籤的說明

語法:- 說明文字git

/**
 * @constructor Student - 學生
 * @param {string} name - 學生的名字
 */
 function Student(name) {
     
 }

4. 類型

語法:{typeName} (可與標籤結合使用,如@param)github

/**
 * @param {string} a 必傳參數
 */
function fn(a, b, c) {

}

5. 可選參數

語法:[paramName] (可與標籤結合使用,如@param)函數

/**
 *  @param {string} a 必傳參數
 *  @param {number} [b] 可選參數
 */
function fn(a, b) {

}

6. 參數有默認值

語法:[paramName=value] (可與標籤結合使用,如@param)this

/**
 *  @param {string} a 必傳參數
 *  @param {number} [c=666] 參數有默認值
 */
function fn(a, c) {

}

7. 連接

語法:[link text]{@link namepathOrURL}google

/**
 * 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、示例

1. 函數

/**
 * 轉換時間字符串爲時間對象
 * @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) {
    // 省略其它代碼
}

2. 類/構造函數

/**
 * 定時器
 * @class Timer
 */

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

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

    /**
     * 定時器是否處於中止狀態
     * @memberof Timer
     * @member stopped
     * @instance
     */
    this.stopped = true;

    /**
     * 啓動定時器
     * @memberof Timer
     * @instance
     * @method start
     * @param {function} handler - 定時器每次執行時調用的函數
     * @param {number} 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();
    }

    /**
     * 中止定時器
     * @memberof Timer
     * @instance
     * @method stop
     */
    this.stop = function () {
        this.stopped = true;
    }
}

/**
 * 監聽事件
 * @memberof Timer
 * @instance
 * @method on
 * @param {string} type - 事件類型  e.g 'stop'
 * @param {function} fn - 事件處理函數
 * @return {number} eventId - 事件處理函數Id,用於取消監聽
 */
Timer.prototype.on = function (type, fn) {
    let _eventId = fn.name || ++this._eventId;
    this.eventHandler[type][_eventId] = fn;
    return _eventId;
}

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

/**
 * 取消監聽事件
 * @memberof Timer
 * @instance
 * @method off
 * @param {string} type - 事件類型  e.g 'stop'
 * @param {function} target - 事件處理函數Id或者函數名字
 */
Timer.prototype.off = function (type, target) {
    let _target = (typeof target === 'function') ? target.name : target;
    delete this.eventHandler[type][_target];
}
相關文章
相關標籤/搜索