8

Is there a way to avoid having to type two seperate lines for @property and @param if, as in the example, on the constructor the parameters and properties are identically named.

/**
 * Class for representing a point in 2D space.
 * @property {number} x The x coordinate of this point.
 * @property {number} y The y coordinate of this point.
 * @constructor
 * @param {number} x The x coordinate of this point.
 * @param {number} y The y coordinate of this point.
 * @return {Point} A new Point
 */
Point = function (x, y)
{
    this.x = x;
    this.y = y;
}

1 Answers1

3

You do not. It is impossible, because the arguments of the function and properties of an object - these are different variables, they can not be both function parameters and object properties. Moreover, such documentation would only confuse the developer, but does not help to understand the API.

I would advise you not to use the @properties in this piece of documentation, but use @type:

/**
 * Class for representing a point in 2D space.
 * @constructor
 * @param {number} x The x coordinate of this point.
 * @param {number} y The y coordinate of this point.
 * @return {Point} A new Point
 */
Point = function (x, y)
{
    /**
     * The x coordinate.
     * @type number
     */
    this.x = x;

    /**
     * The y coordinate.
     * @type number
     */
    this.y = y;
}

But in fact such detailed documentation is useless. What are we talking in code Point.x = x is clear to any schoolchild.

Microfed
  • 2,832
  • 22
  • 25
  • My suggestion is that you only declare jsdoc properties from the constructor as you have (even if it's only going to get assigned later. Avoid creating them from random functions in a class. – Ruan Mendes Apr 26 '13 at 20:02