如何记录具有多个别名的方法?

时间:2018-05-02 00:03:59

标签: javascript documentation jsdoc jsdoc3

我正在尝试记录以下Person构造函数的 getName()方法:

Javascript代码:

/**
 * Creates a person instance.
 * @param {string} name The person's full name.
 * @constructor
 */
function Person( name ) {

    /**
     * Returns the person's full name.
     * @return {string} The current person's full name.
     */
    function getName() {
        return name;
    }

    this.getName = getName;
    this.getN = getName;
    this.getFullName = getName;
}

如您所见, getName()方法有两个别名( getN() getFullName()),所以很明显要使用的标记是@alias标记,但不幸的是,它有两个主要问题:

  

1-它告诉JSDoc 重命名方法。

     

2-它不能用于多个别名

有没有官方方法记录这些方法?

1 个答案:

答案 0 :(得分:1)

这个问题的答案可能听起来有点滑稽,但实际上,有一种官方方法来记录方法别名,他们称之为@borrows

  

@borrows 标记允许您为其他符号添加文档   你的文件。

     

如果你有多种方式来引用a,那么这个标记会很有用   功能,但您不想复制相同的文档   两个地方。

因此, getName()应记录如下:

Javascript代码:

/**
 * Creates a person instance.
 * @param {string} name The person's full name.
 * @constructor
 * @borrows Person#getName as Person#getN
 * @borrows Person#getName as Person#getFullName
 */
function Person( name ) {

    /**
     * Returns the person's full name.
     * @return {string} The current person's full name.
     * @instance
     * @memberof Person
     */
    function getName() {
        return name;
    }

    this.getName = getName;
    this.getN = getName;
    this.getFullName = getName;
}

JSDoc结果:

Result