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; },
开发者_如何学C /** 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; }
}
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
.
You could use jQuery style getter / setter methods:
/**
* Get colour of object
* @returns {mixed}
*//**
* Set colour of object
* @param {mixed} val
* @returns {this}
*/
colour: function(val) {
if (val === undefined)
return this.colour;
else {
this.colour = val;
return this;
}
}
I have just been discussing this very issue with Michael himself. It is possible in jsDoc3 (https://github.com/micmath/jsdoc) due to a very cool feature. It is possible to stack multiple docblocks (one for getter and one for setter):
http://groups.google.com/group/jsdoc-users/browse_thread/thread/d4c7794bc8f6648e/94df7339e1fc4c91#94df7339e1fc4c91
精彩评论