JSDoc 和 JavaScript 属性 getter 和 setter

发布于 2024-11-15 10:21:32 字数 517 浏览 5 评论 0原文

我希望我的代码(如下所示)可以生成使用 JSDoc(v2.4.0) 描述对象文字的每个属性的文档,但它不起作用。有谁知道如何使用 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; }
}

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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-11-22 10:21:32

使用 @type 记录 JavaScript getset 访问器。类似下面的内容应该适用于 JSDoc:

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

这将每个属性记录为具有 number 类型的成员。

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.

昔梦 2024-11-22 10:21:32

您可以使用 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;
    }
}

我刚刚与 Michael 本人讨论了这个问题。由于非常酷,在 jsDoc3 (https://github.com/micmath/jsdoc) 中这是可能的特征。可以堆叠多个文档块(一个用于 getter,一个用于 setter):

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文