Dashiell Rose Bark-Huss
Dashiell Rose Bark-Huss

Reputation: 2975

JsDocs: How do you document methods in a factory function?

I can't get JsDoc documentation to show up on an object's (car) method (car.drive()) when the object is created from a factory function Car(). This is in VSCode.

Factory Function:

/**
 * Creates car object
 * @param {Sting} model name of
 *
 * @returns {Object} car object
 */
function Car(model) {
  this.model = model;

  /**
   * Makes the car drive
   * @param {String} speed speed of car
   */
  this.drive = (speed) => {
    console.log(`Car is moving at ${speed} miles per hour`);
  };
  return this;
}

When I hover my mouse over car.drive() it doesn't show the JSDocs. It just displays Any.

const car = Car({}); // JsDocs for Car show up here
car.drive() // JsDocs for the drive method don't show up here

How can you document this.drive in Car so you could see the JSDocs on car.drive()? Is this possible?

Upvotes: 0

Views: 210

Answers (1)

mylee
mylee

Reputation: 1333

VS Code does recognize JSDoc documentations in JavaScript files. The only thing missing here to make it work is that Car is a constructor and not a function. If you were to initialize a new object of Car it would be to use the code

var car = new Car({});
car.drive();

Look at the documentation for the new operator.

Upvotes: 1

Related Questions