75

编辑:这在技术上是一个两部分的问题。我选择了涵盖一般问题的最佳答案,并链接到处理特定问题的答案。

用 jsdoc 记录匿名对象和函数的最佳方法是什么?

/**
 * @class {Page} Page Class specification
 */
var Page = function() {

    /**
     * Get a page from the server
     * @param {PageRequest} pageRequest Info on the page you want to request
     * @param {function} callback Function executed when page is retrieved
     */
    this.getPage = function(pageRequest, callback) {
    }; 
};

代码中既不存在PageRequest对象也不callback存在。它们将getPage()在运行时提供给。但我希望能够定义对象和函数是什么。

我可以摆脱创建PageRequest对象来记录:

/**
 * @namespace {PageRequest} Object specification
 * @property {String} pageId ID of the page you want.
 * @property {String} pageName Name of the page you want.
 */
var PageRequest = {
    pageId : null,
    pageName : null
};

这很好(尽管我愿意接受更好的方法来做到这一点)。

记录函数的最佳方法是什么callback?我想在文档中说明,比如回调函数的形式是:

callback: function({PageResponse} pageResponse, {PageRequestStatus} pageRequestStatus)

任何想法如何做到这一点?

4

6 回答 6

63

您可以使用 @name 标记记录代码中不存在的内容。

/**
 * Description of the function
 * @name IDontReallyExist
 * @function
 * @param {String} someParameter Description
*/

/**
 * The CallAgain method calls the provided function twice
 * @param {IDontReallyExist} func The function to call twice
*/
exports.CallAgain = function(func) { func(); func(); }

这是@name 标签文档。您可能会发现名称路径也很有用。

于 2010-08-22T20:44:00.353 回答
51

您可以使用@callback@typedef

/**
 * @callback arrayCallback
 * @param  {object} element - Value of array element
 * @param  {number} index   - Index of array element
 * @param  {Array}  array   - Array itself
 */

/**
 * @param {arrayCallback} callback - function applied against elements
 * @return {Array} with elements transformed by callback
 */
Array.prototype.map = function(callback) { ... }
于 2013-07-23T13:10:48.217 回答
9

为了恭维 studgeek 的回答,我提供了一个示例,展示了 带有 Google Closure Compiler 的 JsDoc可以让您做什么。

请注意,记录的匿名类型会从生成的缩小文件中删除,并且编译器会确保传入有效对象(如果可能)。但是,即使您不使用编译器,它也可以帮助下一个开发人员和 WebStorm (IntelliJ) 等工具理解它并为您提供代码补全。

// This defines an named type that you don't need much besides its name in the code
// Look at the definition of Page#getPage which illustrates defining a type inline
/**  @typedef { pageId : string, pageName : string, contents: string} */
var PageResponse;

/**
 * @class {Page} Page Class specification
 */
var Page = function() {    
    /**
     * Get a page from the server
     * @param {PageRequest} pageRequest Info on the page you want to request
     *
     * The type for the second parameter for the function below is defined inline
     *
     * @param {function(PageResponse, {statusCode: number, statusMsg: string})} callback
     *        Function executed when page is retrieved
     */
    this.getPage = function(pageRequest, callback) {
    }; 
};
于 2013-04-18T16:51:36.783 回答
2

@link可以向方法和类添加内联链接。

/**
 * Get a page from the server
 * @param {PageRequest} pageRequest Info on the page you want to request
 * @param {function} callback Function executed when page is retrieved<br />
 * function({@link PageResponse} pageResponse,{@link PageRequestStatus} pageRequestStatus)
 */
this.getPage = function (pageRequest, callback) {
};

不理想,但它可以完成工作。

于 2010-07-06T19:07:55.370 回答
1

Google Closure Compiler Annotations 对此有类型表达式,其中包括指示特定参数、返回类型甚至 this 的类型的能力。许多库都在关注 Google Closure Compiler Annotations,因为他们想用它来压缩他们的代码。所以它有一些动力。缺点是我看不到给出描述的方法。

为了提供描述,也许 JSDoc Toolkit Parameters With Properties方法会起作用(查看页面底部)。这就是我现在正在做的事情。JSDoc Toolkit 正在准备开始在 V3 上工作,因此那里的反馈可能很好。

于 2011-02-23T16:22:00.640 回答
0

您可以使用 @see 链接到同一类中的另一个方法。该方法永远不会被使用,它只是用于文档目的。

/**
 * @class {Page} Page Class specification
 */
var Page = function() {

    /**
     * Get a page from the server
     * @param {PageRequest} pageRequest Info on the page you want to request
     * @param {function} callback Function executed when page is retrieved
     * @see #getPageCallback 
     */
    this.getPage = function (pageRequest, callback) {
    }; 

    /**
     * Called when page request completes 
     * @param {PageResponse} pageResponse The requested page
     * @param {PageRequestStatus} pageRequestStatus Status of the page
     */
    //#ifdef 0
    this.getPageCallback = function (pageResponse, pageRequestStatus) { };
    //#endif 
};

如果您正在使用某种构建系统,则可以轻松地从构建中省略虚拟方法。

于 2010-07-06T01:33:26.923 回答