【问题标题】:How to document module.exports defined with object.defineProperty如何记录用 object.defineProperty 定义的 module.exports
【发布时间】:2016-04-05 02:01:56
【问题描述】:

我正在使用 NodeJS 并试图让 JSDoc 获取我正在做的事情。我有一些看起来像这样的代码:

Object.defineProperty(module, 'exports', {
    enumerable: true,
    configurable: true,
    get: function() {
        const factory = {};

        /**
         * Output message to the console
         * @param {string} str
         */
        factory.foo = function(str) {
            console.log(str);
        };

        return factory;
    }
});

在这种情况下,不能以标准方式exports.foo = function(str) { ... } 导出 foo。

另一个模块可以包含这个模块可以访问 foo(就像它被直接导出一样)。例如:

var x = require('./x');
x.foo('Hello');

那么我怎样才能记录这一点,以便 jsDoc 知道这个模块有一个函数 foo?

【问题讨论】:

    标签: node.js documentation jsdoc


    【解决方案1】:

    我找到了一种似乎可行的方法:

    Object.defineProperty(module, 'exports', {
        enumerable: true,
        configurable: true,
        get: Factory
    });
    
    /**
     * Get a factory object.
     * @returns {Factory}
     * @constructor
     */
    function Factory() {
        var factory = Object.create(Factory.prototype);
    
        /**
         * Output a message to the console.
         * @name Factory#foo
         * @param {string} str
         */
        factory.foo = function(str) {
            console.log(str);
        };
    
        return factory;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2015-06-21
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多