简体   繁体   English

JSDoc 和 JavaScript 属性获取器和设置器

[英]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.我希望我的代码(如下所示)可以使用 JSDoc(v2.4.0)生成描述 object 文字的每个属性的文档,但它不起作用。 Does anyone know how to work with JSDoc to generate documents from code that uses getter/setter?有谁知道如何使用 JSDoc 从使用 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; }
}

Use @type to document JavaScript get and set accessors.使用@type记录 JavaScript getset访问器。 Something like the following should work with JSDoc:像下面这样的东西应该适用于 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 .这将每个属性记录为具有number类型的成员。

You could use jQuery style getter / setter methods:您可以使用 jQuery 样式的 getter / setter 方法:

/**
 * 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.由于一个非常酷的功能,它在 jsDoc3 ( https://github.com/micmath/jsdoc ) 中是可能的。 It is possible to stack multiple docblocks (one for getter and one for setter):可以堆叠多个文档块(一个用于 getter,一个用于 setter):

http://groups.google.com/group/jsdoc-users/browse_thread/thread/d4c7794bc8f6648e/94df7339e1fc4c91#94df7339e1fc4c91 http://groups.google.com/group/jsdoc-users/browse_thread/thread/d4c7794bc8f6648e/94df7339e1fc4c91#94df7339e1fc4c91

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM