JSDoc并使用函数记录返回的对象文字

时间:2014-07-31 10:47:32

标签: javascript jsdoc object-literal

我目前使用JSDoc来记录我的AMD模块和类定义。例如:

define([], function() {

    /**
     * @class Mocks HTTP requests
     * @name test/xhr/RequestMocks
     * @param {String} method
     */
    function RequestMocks(method) {
        this.method = method;
    }

    /**
     * Adds a mock
     * @param {String} url
     * @returns {RequestMockConfig}
     * @memberOf test/xhr/RequestMocks
     */
    RequestMocks.prototype.addMock = function(url) {    
        var mock = {
            url: url,
            status: 200
        };

        // TODO register this mock somewhere

        /*
         * WHAT TO TYPE HERE? 
         * I would like to name this object "RequestMockConfig"
         * and have it appear on test/xhr/RequestMocks JSDoc page
         */
        return {
            /**
             * Mocks the http response status
             * @param {Number} status
             * @returns {RequestMockConfig}
             */
            status: function(status) {
                mock.status = status;
                return this;
            },
            /**
             * Mocks the http response body
             * @param {Object} response
             * @returns {RequestMockConfig}
             */
            response: function(response) {
                mock.response = JSON.stringify(response);
                return this;
            }
        };
    };

    return RequestMocks;

});

我会这样使用它:

var mockedGetRequests = new RequestMocks('GET');
mockedGetRequests.addMock('/my/url')
    .status(404)
    .response({ error: 'Not found' });
mockedGetRequests.addMock('/my/url2')
    .response({ data: 'my data' });

记录返回的对象文字RequestMockConfig及其函数status()response()的正确方法是什么?

我知道@typedef标签,但它似乎只对定义不包含任何功能的简单对象有用。

0 个答案:

没有答案