Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly document instance members added via Object.defineProperties?

I have a class which defines a few instance properties via Object.defineProperties and I'm having great difficulty getting JSDoc 3 to recognize that they belong to their class.

Here's a simplified version of what I'm working with:

/** @exports mymodule */
function mymodule(exports) {
    /** @constructor
      * @param {String} foo A foo.
      * @param {String} bar A bar.
      * @classdesc Has a foo and a bar.
      */
    function Example(foo, bar) {
        Object.defineProperties(this, {
            /** A foo and a bar
              * @memberof Example
              */
            foobar: { enumerable: false, value: foo + bar, writable: false }
        });
    }

    exports.Example = Example;
}

When I run JSDoc, I get output for mymodule, Example, foo, and bar, but not foobar. If I remove the @memberof tag for foobar, it get registered as a global. I've tried @memberof mymmodule~Example, adding @lends to both the Object.defineProperties call and the object passed to it, and converting it to Object.defineProperty, but the results don't change.

How can I document foobar as belonging to Example?

like image 265
Ben Blank Avatar asked Sep 17 '12 05:09

Ben Blank


People also ask

What is object defineProperties?

defineProperty() The static method Object. defineProperty() defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

What is the syntax of object defineProperty () method in Javascript?

Syntax: Object. defineProperty(obj, prop, descriptor)

What is the difference between GET and defineProperty?

defineProperty vs get/set notation is that defineProperty can be used at any time, even on objects that have already been created (on which you cannot use the shorthand 'get' notation). From the mdn: To append a getter to an existing object later at any time, use Object.


2 Answers

After digging through every example I could find, I finally assembled the necessary incantation — @memberof is indeed the trick, but JSDoc seems to require that modules being used in namepaths be explicitly marked as such. The following worked perfectly:

/** A foo and a bar
  *
  * @type String
  * @instance
  * @memberof module:mymodule~Example
  */
like image 99
Ben Blank Avatar answered Nov 15 '22 03:11

Ben Blank


You could also try the @lends annotation in place of @memberOf like this :

Object.defineProperties(this, /** @lends Example# */{
    /** A foo and a bar */
    foobar: { enumerable: false, value: foo + bar, writable: false }
});

Don't forget the sharp symbol after the class name to tell jsdoc members are instance members and not static members.

like image 23
Bludwarf Avatar answered Nov 15 '22 04:11

Bludwarf