I am trying to document my code using JSDoc-toolkit. My code starts by being wrapped with a self-executing anonymous function. How in the world do I document this? I've spent nearly all day on this. JS Docs will not recognize anything inside of the anonymous function closure due to it not knowing what to do with it. It breaks and none of my comments come through.
My code looks something like this.
/** * @fileoverview BLA BLA BLA */ /** * This is where I don't know what to put. */ (function () { "use strict"; /** or here */ var stlib = function (param, param, param) { /** or here */ var share = { /** or here */ config: { button: DOM Element, property: blablabla }, init: function () { ...some init code here} }; share.init(); }; widgets.add("share", stlib); }());
Thank you!
Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.
ES6 introduced a new and shorter way of declaring an anonymous function, which is known as Arrow Functions. In an Arrow function, everything remains the same, except here we don't need the function keyword also. Here, we define the function by a single parenthesis and then '=>' followed by the function body.
Anonymous functions are often arguments being passed to higher-order functions, or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function.
An anonymous function in javascript is not accessible after its initial creation. Therefore, we need to assign it to a variable, so that we can use its value later. They are always invoked (called) using the variable name. Also, we create anonymous functions in JavaScript, where we want to use functions as values.
You can use @namespace with @name and @lends like this:
/** * @name MyNamespace * @namespace Hold all functionality */ (function () { "use strict"; /** @lends MyNamespace*/ var stlib = function (param, param, param) { ...All of my code...}; }());
You can't document nested functions directly. But you can do something like this:
/** * @module foobar */ /** * @function * @author Baa * @name hello * @description Output a greeting * @param {String} name - The name of the person to say hello */ (function hello(name) { /** * @function * @author Baz * @inner * @private * @memberof module:foobar * @description Check if the argument is a string (see: {@link module:foobar~hello}) * @param {String} string - The string * @returns {String} Returns true if string is valid, false otherwise */ var isString = function checkString(string) { return typeof string === 'string'; }; if (isString(name)) console.log('Hello ' + name + '!'); }('Mr. Bubbles'));
Here I'm setting checkString
as private and inner to be descriptive (since nested functions can't be described), And then I pass in -p
to document private functions. Finally, I add a link to the parent function for reference.
I think jsdoc
is unnecessarily finicky and needs to be replaced with something better. It's a port of javadoc
, so it has a lot of things that are relevant to Java but not JS, and vice versa. There are very common JS idioms, like closures or nested functions, that are hard or impossible to document.
I always check my namepaths and debug using the --explain
flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With