-->

What JSDoc tags to use when creating objects with

2019-09-01 06:49发布

问题:

My CommonJS module uses a factory style object creation (.create()) and I try to have JSDoc style documentation but cannot figure out which JSDoc tags @class, @module etc. I should to use to do it nicely. Below you can see a simplified structure of the module.

var MyLib = function () {
  this.msg = 'Hello';
};

exports.create = function () {
  return new MyLib();
};

MyLib.prototype.greet = function () {
  console.log(this.msg);
};

The lib is used in the following way. User does not know and does not need to know anything about how the lib works behind mylib.create().

var mylib = require('mylib');
var a = mylib.create();
a.greet(); // 'Hello'

Therefore my question is, how should I tag the functions to have documentation to include mylib.create() as a constructor, mylib.instance.greet() as a method and not include anything about MyLib object that is used only inside the module.

回答1:

I would say @constructs is what you are looking for (see also http://usejsdoc.org/tags-constructs.html).

But I would argue that should document MyLib as a class and create as a function which returns an instance of that class with a proper link (i.e. @see).