Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSDoc and JavaScript property getters and setters

I was hoping that my code, something like below, could generate documents describing each property of the object literal with JSDoc(v2.4.0), but it did not work. Does anyone know how to work with JSDoc to generate documents from code that uses getter/setter?

/** Enum of days of week. */
var Day = {
    /** Sunday. */
    get Sun() { return 0; },
    /** Monday. */
    get Mon() { return 1; },
    /** Thuesday. */
    get Tue() { return 2; },
    /** Wednesday. */
    get Wed() { return 3; },
    /** Thursday. */
    get Thu() { return 4; },
    /** Friday. */
    get Fri() { return 5; },
    /** Saturday. */
    get Sat() { return 6; }
}
like image 235
enobufs Avatar asked Jun 14 '11 04:06

enobufs


People also ask

Are there getters and setters in JavaScript?

JavaScript Accessors (Getters and Setters)ECMAScript 5 (ES5 2009) introduced Getter and Setters. Getters and setters allow you to define Object Accessors (Computed Properties).

What is the difference between getter and setter in JavaScript?

Getters and setters allow us to define Object Accessors. The difference between them is that the former is used to get the property from the object whereas the latter is used to set a property in an object.

What is the benefit of using properties with getters and setters?

The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement.

Is @property a getter?

@property is used to get the value of a private attribute without using any getter methods. We have to put a line @property in front of the method where we return the private variable. To set the value of the private variable, we use @method_name.


1 Answers

Use @type to document JavaScript get and set accessors. Something like the following should work with JSDoc:

    /**
     * Sunday.
     * @type {number}
     */
    get "Sun"() { return 0; },
    /**
     * Monday.
     * @type {number}
     */
    get "Mon"() { return 1; },

This documents each property as a member with a type of number.

like image 193
jackvsworld Avatar answered Sep 18 '22 22:09

jackvsworld