Here is a constructor function A
that gives instances 2 methods: printThing
and printBall
. I use JSDoc to document methods like this:
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++
}
};
};
Here is an equivalent constructor function that dynamically generates the same methods like this:
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');
}
The behaviour of the instances generated by the two constructor functions are identical:
> var a = new A();
> a.printBall(4);
Ball
Ball
Ball
Ball
How can I use JSDoc to document the generated methods in the second A
constructor function?
EDIT: registerPrinter
is private within the scope of the constructor function. It can (and should) be documented, but it's just used internally. This question is about documenting the resulting public interface of A
instances.