简体   繁体   English

jsdoc:如何获取实例方法参数?

[英]jsdoc: How do I get instance method parameters to appear?

I'm trying to document some old code with JSDoc3, and I'm stuck trying to get it to include in the documentation the parameters to instance methods - or to show anything as an instance property at all. 我试图用JSDoc3记录一些旧代码,而我却试图使它在文档中包含实例方法的参数-或根本不显示任何实例属性。 I suspect the problem is that the code does not follow the expected idiom for faking classes in javascript, but I want to get everything documented before I start rewriting anything. 我怀疑问题在于代码没有遵循仿造javascript类的预期习惯,但是我想在开始重写任何内容之前先记录所有内容。 I've tried to make a small example of the problem, with the structure of the actual code: 我试图用实际代码的结构来举一个小问题的例子:

/**
 * Global function
 * @param  {Object} v Stuff that they're trying to avoid making global
 * @return {Object}   Updated v
 */
jsdoc_test = function( v ) {
    /**
     * Some stuff is defined in this namespace
     * @namespace space
     */
    var space = {};
    /**
     * Something that acts like a class
     * @name space.someclass
     * @memberOf space
     * @constructor
     * @type {function}
     * @param  {any}    y blah blah
     * @return {Object}   The constructed object
     */
    space.someclass = function( w ) {
        var obj = {
            source:  w,        // might need this again
            derived: foo( w ), // what we usually need
            etc:     "etc"     // and so on
        };
        /**
         * This should be a member function, but it appears as a static property
         * @name space.someclass.methodA
         * @memberOf space.someclass
         * @type {function}
         * @instance
         * @param  {any}    x Parameters do not appear in documentation
         * @return {Object}   this
         */
        obj.methodA = function( x ) {
            bar( x ); // or whatever methodA does
            return this;
        }
        /**
         * This should be a member function, but it doesn't show up at all
         * @name space.someclass.methodB
         * @memberOf space.someclass#
         * @type {function}
         * @param  {any}    y Parameters do not appear in documentation
         * @return {Object}   this
         */
        obj.methodB = function( y ) {
            baz( y ); // or whatever methodB does
            return this;
        }
        return obj;
        /**
         * This should be a member function, but it doesn't show up at all
         * @name space.someclass.methodC
         * @memberOf space.someclass.prototype
         * @type {function}
         * @param  {any}    z Parameters do not appear in documentation
         * @return {Object}   this
         */
        obj.methodC = function( z ) {
            qux( z ); // or whatever methodC does
            return this;
        }
        return obj;
    }
    // ...
}

I want all three methods to appear in the generated documentation as instance methods. 我希望所有这三种方法都作为实例方法出现在生成的文档中。 As it is, methodA appears as a static property, while methodB and methodC (which follow suggestions from here ) do not appear at all 实际上, methodA看起来是静态属性,而methodBmethodC (遵循此处的建议)根本不出现

How do I get JSDoc3 to document instance methods, with their parameters, without rewriting the code? 如何在不重写代码的情况下使JSDoc3用实例参数记录实例方法?

Looks like you're using too many tags on your code. 看起来您在代码上使用了太多标记。 When you use @constructor , you shouldn't need @name or @type , since those are covered by using constructor. 使用@constructor ,您不需要@name@type ,因为使用构造函数可以覆盖这些内容。

So, you've got two options, I think. 因此,我认为您有两种选择。

Use @constructor and remove the redundant (conflicting) tags: 使用@constructor并删除冗余(冲突)标签:

/**
 * Something that acts like a class
 * @constructor space.someclass
 * @memberOf space
 * @param  {any}    y blah blah
 * @return {Object}   The constructed object
 */

Or, if you don't want to use the @constructor tag, add the appropriate hinting yourself: 或者,如果您不想使用@constructor标记,请自己添加适当的提示:

/**
 * Something that acts like a class
 * @name space.someclass
 * @memberOf space
 * @kind class
 * @param  {any}    y blah blah
 * @return {Object}   The constructed object
 */

In both cases, @type is redundant since you're documenting a class; 在这两种情况下, @type都是多余的,因为您正在记录一个类。 the type would technically be the full name of your function (ie, @type {space.someclass} ). 从技术上讲,类型将是函数的全名(即@type {space.someclass} )。

A combination of @instance, @memberOf & @method should do it: @ instance,@ memberOf和@method的组合应该可以做到:

/**
 * This should now be a member function.
 * @instance
 * @memberOf space.someclass
 * @method methodA
 * @param {*} x Some parameter of any type.
 * @return {Object} this.
 */

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

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