jsdoc - 如何将 jsdoc 与工厂函数一起使用?

标签 jsdoc jsdoc3

所以我有一个导出工厂函数的模块。工厂函数接受设置并返回绑定(bind)在该设置对象上的库。我一生都无法弄清楚如何使用 jsdocs 来记录这一点;我一直在研究命名空间、typedef 和 memberof,直到我头晕目眩。无论我做什么,它只是没有将函数列为库定义的一部分。帮忙?

如果我完全删除memberof,我可以让它们显示为全局函数,但到目前为止我没有尝试过让它们显示为成员函数

示例代码:

/**
 * @namespace ServerControl
 */
/**
 * @typedef {Object} Library
 */

/**
 * Factory function that constructs a lib
 * @param {*} settings Settings for constructing a lib
 * @param {*} rancher The rancher library to be used by the lib
 * @returns {ServerControl~Library} The lib
 */
module.exports = function(settings, rancher) {
    return {
        /**
         * Evacuate a host
         * @memberof {...ServerControl~Library}
         * @method evacuate
         * @param {String} name The name of the host to evacuate
         * @returns {Promise} A promise that fulfills when the evacuation is done
         */
        evacuate: name => {
            const server = settings.servers.filter(item => item.display === name)[0];
            return rancher.evacuateHost(server.host, server.env);
        }
        // [more methods snipped]
    }

最佳答案

我有同样的问题,我的模块总是暴露工厂函数或函数映射。我总是在模块包装器之外使用 @typedef。这样我就可以在 IDEA 中完成代码并且可以生成漂亮的 HTML 文档。

Here the generated documentation对于这个例子:

/** @namespace SharedLib */

/**
 * @typedef SharedLib.PriorityQueueFactory
 * @function
 * @template T
 * @param {function(T, T): Boolean} comparator Comparison function like for <code>Array.prototype.sort</code>
 * @return {{pop: function(Array<T>): Array<Array<T>| T>, push: function(Array<T>, T): Array<T>}} an object containing the functions to manage the queue
 */

// UMD wrapper - sorry!
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define([], factory);
    }
    else if (typeof module === 'object' && module.exports) {
        module.exports = factory();
    }
    else {
        root.returnExports = factory();
    }
}(typeof self !== 'undefined' ? self : this,
    function () {

        /** @type {SharedLib.PriorityQueueFactory} */
        function priorityQueueFactory(comparator) {
            // ...
    return priorityQueueFactory;
}));

关于jsdoc - 如何将 jsdoc 与工厂函数一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48949013/

相关文章:

jsdoc - 原始类型名称需要大写还是小写?

javascript - jslint、jsdoc 和 rhino 集成以从命令行运行?

javascript - 将子属性添加到 jsdoc 中的现有属性列表

javascript - 静态类属性不适用于 Babel

jsdoc - 如何在JsDoc中忽略源链接?

javascript - JSDoc:在另一个@param 中引用方法的@param

javascript - 我如何在 Windows 上使用 JSDoc?

javascript - 直接从源代码生成 JavaScript 文档

javascript - Google 的 JS API 文档使用什么文档标准?

javascript - JSDoc中如何注解 "@readonly-but-modified-internally"成员/属性?