10

Most of my Javascript functions are relatively simple, and called for their sideeffects: I use jQuery to manipulate the DOM or make Ajax-calls. I prefer to write my functions in the "revealing module pattern" style.

I just discovered that JSDoc- annotating Javascript files has a benefit: with the help of the annotations, Eclipse's JS Development Tools can parse my JS file and fill the Eclipse Outline View (which would otherwise be empty).

Now I wonder what are the fine points, or the good practices of annotating? I am not used to it.

The google JS style guide says something about JSDoc: recommends to only use a subset of available tags, among other advice.

For now, I came up with this template (this code does not do anything useful):

/**
 * @fileOverview Say something meaningful about the js file.
 * @author <a href="mailto:my@email.net">My name</a>
 * @version 1.0.1
 */


/**
 * @namespace What the namespace contains or which apps/webpages use it
 */
if (!window['my']['namespace']) {

    window['my']['namespace'] = {};    
my.namespace = (function() {
    /**
     * Documentation string...
     * @memberOf window.my.namespace
     * @private 
     */
    var clear = function(){};


    /**
     * Documentation string...
     * @memberOf window.my.namespace
     * @public 
     */
    function delete_success(data){
        var str = "# of files affected: " + data.length; 
        $('<pre id="success"/>').html(str).appendTo('#del_0b');
        $('<pre id="success"/>').html(data.result).appendTo('#del_sf');
    }
//more code


  return {
      "method1": method1,
      "delete_success" : delete_success
      };
   })();    //my.namespace
} //end if

Am I supposed to use JSDoc tag @function or @memberOf here, or both? What about the @field tag? Should the return clause be JSDoc'umented as well? With which tags? Should I really not use the @public tag? I find it useful here.

Any recommendations? Does anyone know a good, practical JSDoc style guide for small projects?

Community
  • 1
  • 1
knb
  • 9,138
  • 4
  • 58
  • 85
  • Have you figured out something more yet? It seems like `@memberOf` is the only tag that does anything to the outliner. I don't get `@module` or `@namespace`. Although the names are pretentious, they don't really do anything. – Redsandro Jan 14 '13 at 17:05
  • @Redsandro: no I haven't anything new, sorry – knb Jan 14 '13 at 17:50

3 Answers3

3

If you're looking for code samples, I've found that the best place to find them is in the archives of the jsdoc-users Google Group. I've had much better luck there than searching Google, and if you ask a question they're usually pretty good about helping out.

I can't speak for the Eclipse support, but there is a new version of jsdoc, jsdoc3. Check out the documentation here. It's a little incomplete, but I know they have updates written and ready for review, so they should be improving soon.

Regarding your specific question regarding @function and @memberof, you'll likely want to use @function, not @memberof for simple function documentation.

Dan
  • 151
  • 1
  • 5
  • 3
    yeah, but in the reveling module pattern these functions are methods of my.namespaced.object, so memberOf could also be used. meanwhile, I use whatever makes something of the structure appear in Eclipse's outline view. In any case, this question does not ask for a solution for an immediate problem of mine, the question has a more long-term scope. – knb Mar 05 '13 at 09:18
0

In Eclipse @memberOf (with capital O) does the trick for the outline (Ctrl+O shortcut). I use JSDoc mostly for the Eclipse outline, but I also use @author for humans :)

I also use @private on private functions.

IMHO JSDT is OK but not very helpful, it did not evolve much lately. You should use an Eclipse JSHint plugin or use TypeScript with an Eclipse plugin (you can do refactoring but adds some complexity).

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
0

for me (Eclipse 4.3 Kepler) the following works fine:

my.namespace.foo.AbstractClass = {

  /** @memberOf my.namespace.foo.StaticClass  <- this statement already 
   *                   fixes the Eclipse Outline and Package Views for all other members
   */
  staticMethod1 : function() { /* ... */ },

  /** no need to add some JSDoc here for the Outline etc. */
  staticMethod2 : function() { /* ... */ }
}

(for "non-abstract" classes, speaking Java, it should be similar)

which is nice because:

  • it's almost the minimum to not repeat the namespace or JSDoc tags all over
  • I do not have to fiddle around with prototype or this
  • the "abstract class"1 is immediately created

1: I know - everything is object - but I feel better with stronger typed and namespaced environments/clearer/more defined concepts like in Java. The whole JavaScript stuff is just grown and (IMHO) really bad and hard to work with in bigger environments with multiple programmers and solid refactoring support, good maintainability, testability, modularity, dependency management, self documentation etc.

Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96