【发布时间】:2016-03-16 16:52:18
【问题描述】:
这是一个构造函数A,它为实例提供了两种方法:printThing 和printBall。我使用JSDoc 来记录这样的方法:
var A = function () {
/**
* Prints 'Thing'
* @param {Number} N - The number of times to print.
*/
this.printThing = function (N) {
var i = 0;
while (i < N) {
console.log('Thing');
i++
}
};
/**
* Prints 'Ball'
* @param {Number} N - The number of times to print.
*/
this.printBall = function (N) {
var i = 0;
while (i < N) {
console.log('Ball');
i++
}
};
};
这是一个等效的构造函数,它可以像这样动态生成相同的方法:
var A = function () {
var me = this;
var registerPrinter = function (name) {
me['print' + name] = function (N) {
var i = 0;
while (i < N) {
console.log(name);
i++;
}
};
};
registerPrinter('Thing');
registerPrinter('Ball');
}
两个构造函数生成的实例的行为是相同的:
> var a = new A();
> a.printBall(4);
Ball
Ball
Ball
Ball
如何使用 JSDoc 记录第二个 A 构造函数中生成的方法?
编辑:registerPrinter 在构造函数范围内是私有的。它可以(并且应该)记录在案,但它只是在内部使用。这个问题是关于记录A 实例的公共接口。
【问题讨论】:
-
我发现这个文档很有用:usejsdoc.org/about-namepaths.html
标签: javascript jsdoc