Question

I'm beginning a fairly large JavaScript project. The project will have everything exist within one or more namespaces, so only a sparse number of things will exist in the global scope (at the moment, there is only one thing).

In order to keep things organized, I would like to keep each class in its own file (much like you would other many other languages) instead of in "modules".

I built a little compilation script (with NodeJS) which will compile all of the files in to a single JavaScript file at the end of the day.

I thought I had a good method, but I'm trying to get JSDoc(3) to cooperate with me and not having a lot of luck, so I'm wondering if I'm maybe tackling this in the wrong way, or if there is some trick with JSDoc I'm missing.

In the source, each namespace is a folder and each class is a file of the same name.

Each class file looks something like this:

var BaseNS = BaseNS || {};
BaseNS.SubNS = BaseNS.SubNS || {};

BaseNS.SubNS.MyClass = (function() {
    function MyClass() {
    }

    Object.defineProperties(MyClass.prototype, {
       'someProp', { 
            getter: function() { return this._someProp = this._someProp || 0; },
            setter: function(val) { this._someProp = val; }
       }
       // ... more
    });

    MyClass.prototype.someMethod = function() {};
    // ... more

    return MyClass;
}}());

When compiled together, my compilation script handles dependencies to get the classes in the correct order and also removed duplicate namespace declaration lines.

I think this method is relatively clean and straight-forward. The catch is, I can't figure out how to write the JSDoc documentation in a way which gives me things as I think they should be.

I would like to go with a "class-like" method for things. This is actually for an application which is largely removed from the DOM. Also, I'd like the entire project to be vanilla, modern JavaScript (i.e., no RequireJS, etc.)

Any ideas how to get this documented, or a better way to arrange things?

Thanks.

Was it helpful?

Solution

After playing around with various things for about four hours yesterday, I think I've figured out the optimal format, which combines nice efficent form while allowing for minimal comments to get things to parse properly.

"use strict";

/** @namespace */
var NS = NS || {};

/** @namespace */
NS.Sub = NS.Sub || {};

/**
 * @class
 */
NS.Sub.SomeClass = (function(NS) {
    /**
     * @constructor
     * @lends NS.Sub.SomeClass
     */
    var SomeClass = function() {
        /** @method */
        this.someFunc = function(cookie) {

        };

        Object.defineProperties(this, /** @lends NS.Sub.SomeClass */ {
            /**
             * @type {number}
             * @instance
             */
            somePublicProp: {
                getter: function() { return this._somePublicProp || 24; },
                setter: function(val) { this._somePublicProp = val; }
            }
        });
    };

    return SomeClass;
})(NS);

If anyone has any recommendations on how to make this better, I'd love to hear them. In particular, I can't figure out a way to get rid of the need for var SomeClass; ... return SomeClass. I'd like to just return it directly (preferably without any name), but then JSDoc loses track of my method, which means more comments, which isn't cool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top