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
?
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
?
4 Answers
Reset to default 8After 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.
After fiddling around for many hours, I finally got it to work with @memberOf!
. Note the uppercase "O" and the bang "!"
/**
* Description
* @memberOf! MyClass
* @type {String}
*/
var myString;
In case you want to use defineProperty
instead, a @type
annotation is sufficient (at least for IntelliJ to infer the proper type for the property):
/** @type SomeType */
Object.defineProperty(this, "someProperty", {
get: () => new SomeType()
});