JSDoc 中文网

语法

¥Syntax

@callback <namepath>

概述

¥Overview

@callback 标签提供有关可传递给其他函数的回调函数的信息,包括回调的参数和返回值。你可以包含可为 @method 提供的任何标签。

¥The @callback tag provides information about a callback function that can be passed to other functions, including the callback's parameters and return value. You can include any of the tags that you can provide for a @method.

定义回调后,你可以按照与使用 @typedef 标记定义的自定义类型相同的方式使用它。特别是,你可以使用回调的名称作为类型名称。这允许你指示函数参数应包含某种类型的回调。

¥Once you define a callback, you can use it in the same way as a custom type defined with the @typedef tag. In particular, you can use the callback's name as a type name. This allows you to indicate that a function parameter should contain a certain type of callback.

如果你希望显示带有特定类的类型定义的回调,你可以为回调提供一个名称路径,表明它是该类的内部函数。你还可以定义从多个类引用的全局回调类型。

¥If you want a callback to be displayed with the type definitions for a specific class, you can give the callback a namepath indicating that it is an inner function of that class. You can also define a global callback type that is referenced from multiple classes.

示例

¥Examples

记录特定于类的回调
/**

 * @class
 */
function Requester() {}

/**

 * Send a request.

 * @param {Requester~requestCallback} cb - The callback that handles the response.
 */
Requester.prototype.send = function(cb) {
    // code
};

/**

 * This callback is displayed as part of the Requester class.

 * @callback Requester~requestCallback

 * @param {number} responseCode

 * @param {string} responseMessage
 */
记录全局回调
/**

 * @class
 */
function Requester() {}

/**

 * Send a request.

 * @param {requestCallback} cb - The callback that handles the response.
 */
Requester.prototype.send = function(cb) {
    // code
};

/**

 * This callback is displayed as a global member.

 * @callback requestCallback

 * @param {number} responseCode

 * @param {string} responseMessage
 */