Node.js 如何记录使用object.defineProperty定义的module.exports
我正在使用NodeJS并试图让JSDoc属性了解我在做什么。我有一些代码如下所示:Node.js 如何记录使用object.defineProperty定义的module.exports,node.js,documentation,jsdoc,Node.js,Documentation,Jsdoc,我正在使用NodeJS并试图让JSDoc属性了解我在做什么。我有一些代码如下所示: Object.defineProperty(module, 'exports', { enumerable: true, configurable: true, get: function() { const factory = {}; /** * Output message to the console * @par
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;
}
});
以标准方式导出fooexports.foo=function(str){…}
在这种情况下不是选项。
另一个模块可以包括该模块,该模块可以访问foo(就像它是直接导出的一样)。例如:
var x = require('./x');
x.foo('Hello');
那么,我如何记录这一点,以便jsDoc能够识别出这个模块有一个函数foo?我找到了一种似乎可行的方法:
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;
}