javascript - 使用 JSDoc 记录工厂

标签 javascript factory jsdoc

为了避免在我的 JavaScript 代码中使用 new,我编写了工厂来创建对象。

我尝试了很多组合,给我最满意的结果如下:

/**
 * Document module
 * @module app/document
 */
(function () {
    'use strict';

    /**
     * Factory that creates a document object.
     * @alias module:app/document.factory
     * @return {document}
     */
    function document() {
        /**
         * Get document id
         * @method id
         * @return {String}
         */
        var id = function id() {...},
            api = {
                id: id
            };

        return api;
    }

    /**
     * This module exports the {@link module:app/document.factory|factory} function.
     */
    module.exports = document;
}());

这些注释的问题是没有定义document 对象。因此,我无法在另一个对象中引用此对象,并且在扩展此对象时无法继承其文档。

记录这种对象的合适方法是什么?

如果我使用 @typedef 标签,我会得到静态的 factory 方法 并且正确记录了 document 对象 但是 JSDoc 没有生成 id 方法文档:

/**
 * Document module.
 * @module app/document
 */
(function () {
    'use strict';

    /**
     * Factory that creates a document object.
     * @function module:app/document.factory
     * @return {document}
     */
    function factory(agent) {
        /**
         * @callback document~id
         * @returns {String}
         */
        var id = function id() {...},

            /**
             * @typedef document
             * @property {document~id} id
             */
            document = {
                id: id
            };

        return document;
    }

    module.exports = factory;
}());

最佳答案

我对您的建议是使用@typedef 定义类型并在模块上定义导出,然后使用@type {FactoryDe​​finition} 注释 module.exports = factory

 /** @typedef {{ id: !string }} */
 var DocumentDefinition;

 /** @typedef {!function(!object):!DocumentDefinition} */
 var FactoryDefinition;

/** @type {FactoryDefinition} */
module.exports = factory

关于javascript - 使用 JSDoc 记录工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548126/

相关文章:

javascript - WebUSB 无法连接,因为另一台设备正在使用相同的接口(interface)

java - 抽象工厂模式接口(interface)实现或类扩展

c# - 使用 Is/As 运算符的工厂方法

javascript - 使用 JSDoc 注释通用 ES6 类

javascript - 闭包编译器的正则表达式类型

c# - 选定索引已更改

javascript - 当 firebug 说它是 34 时,为什么 jquery 会为 offsetHeight 返回 0?

JavaScript 不显示警报

python - 连接失败后扭曲延迟不可调用

javascript - _以 "revealing module pattern"样式编写的 JSDoc'ing Javascript 文件的最佳实践?