JSDoc: How do I document `var self = this;`?

JSDoc is great at picking up on methods and properties defined on a class as long as you use the this keyword, e.g.:

/** @class */
function Person(name) {
  /** This person's name */
  this.name = name;

  /** Greet someone */
  this.greet = function(person) {
    return 'Hey there, '+person.name;
  };

  /** Log a greeting to the browser console */
  this.logGreeting = function(person) {
    console.log(this.greet(Person));
  };
}

This will generate docs with a “Class Person” page listing name as a Member and greet() and logGreeting() as Methods.

But when I have a complex case where I need to use the self = this pattern, things start to break:

/** @class */
function Person(name) {
  var self = this;

  /** This person's name */
  self.name = name;

  /** Greet someone */
  self.greet = function(person) {
    return 'Hey there, '+person.name;
  };

  /** Log a greeting to the browser console */
  self.logGreeting = function(person) {
    console.log(self.greet(Person));
  };

  /** Log a greeting to the browser console after some delay */
  self.logGreetingDelayed = function(person, delay) {
    setTimeout(function() { self.logGreeting(person); }, delay);
  };
}

This example generates a Class Person page, but it doesn’t have the name member or any of the methods.

I’ve seen that you can use @memberof all over the place to manually attach each member and method to the class, but this is really verbose, and I was wondering whether there’s a way to tell JSDoc that self refers to the class.

(Btw, I’m using JSDoc 3.4, in case that’s important.)

47 thoughts on “JSDoc: How do I document `var self = this;`?”