Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I document immediate invocation + anonymous function?

Using JSDoc, how do I document this code?

var mynamespace = {};

/**
 * What do I put here?
 */
mynamespace.myfunc = (function () {
    var i = 0;
    /**
     * Do I need to put anything here?
     */
    return function (a) {
        return a + i++;
    };
}());

The signature in this case could be:

/**
 * @param {Number} base
 * @return {Number}
 */

I'm using the Google Closure Compiler and it doesn't like JSDoc near return function () {. I'm guessing there must be a proper way to do this. I guess my real question is: "how do I shut up GCC?" :p

I looked around a bit but I didn't find this situation exactly. I would imagine it's quite common.

like image 921
Halcyon Avatar asked Sep 02 '26 12:09

Halcyon


1 Answers

I'm not a JSDoc pro but I could suggest you something

First I suggest initializing the i variable to 0 to not have a NaN value return by your function. But that's just details of the code and not relevant for your question about commenting according to JSDoc.

Maybe you should organize your code this way to "shut up" GCC :

var mynamespace = {};

/**
 * Display the value we pass with a counter incremented each time we call the method
 * @name myFunc
 * @params {Number} a - value to add to the counter
 * @return {Number} addition of a and i++
 */
mynamespace.myfunc = (function () {
    var i;
    /**
     * @alias myFunc
     */
    function add(a) {
        return a + i++; 
    };
    return add;
}());

It could be because you return an anonymous function.

Hope it helps.

EDIT :

I found out that the @alias tag could make the deal. Maybe you can try with the code I edited in the code snippet.

More details

like image 100
Ganbin Avatar answered Sep 05 '26 00:09

Ganbin