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
?
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.
Syntax: Object. defineProperty(obj, prop, descriptor)
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.
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
*/
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.
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