使用 jsdoc 记录匿名对象和函数的最佳方法

发布于 2024-09-07 22:04:02 字数 1104 浏览 3 评论 0原文

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

使用 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: function({PageResponse} pageResponse, {PageRequestStatus} pageRequestStatus)

有什么想法如何做到这一点?

Edit: This is technically a 2 part question. I've chosen the best answer that covers the question in general and linked to the answer that handles the specific question.

What is the best way to document anonymous objects and functions with 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) {
    }; 
};

Neither the PageRequest object or the callback exist in code. They will be provided to getPage() at runtime. But I would like to be able to define what the object and function are.

I can get away with creating the PageRequest object to document that:

/**
 * @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
};

And that's fine (though I'm open to better ways to do this).

What is the best way to document the callback function? I want to make it know in the document that, for example, the callback function is in the form of:

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

Any ideas how to do this?

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

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

发布评论

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

评论(7

冷︶言冷语的世界 2024-09-14 22:04:02

您可以使用 @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 标签文档。您可能会发现名称路径也很有用。

You can document stuff that doesnt exist in the code by using the @name tag.

/**
 * 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(); }

Here is the @name tag documentation. You might find name paths useful too.

淤浪 2024-09-14 22:04:02

您可以使用 @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) { ... }

You can use @callback or @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) { ... }
甜中书 2024-09-14 22:04:02

为了补充 studgeek 的回答,我提供了一个示例,展示了带有 Google Closure 的 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) {
    }; 
};

To compliment studgeek's answer, I've provided an example that shows what JsDoc with Google Closure Compiler lets you do.

Note that the documented anonymous types get removed from the generated minified file and the compiler ensures valid objects are passed in (when possible). However, even if you don't use the compiler, it can help the next developer and tools like WebStorm (IntelliJ) understand it and give you code completion.

// 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) {
    }; 
};
心房敞 2024-09-14 22:04:02

希望我在这个话题上还不算太晚,但是你可以做这样的事情,

/**
  * Get a page from the server
  * @param {PageRequest} pageRequest
  * @param {(pageResponse: PageResponse, pageRequestStatus: PageRequestStatus) => void} callback
  */
this.getPage = function(pageRequest, callback) {}

这种方法的缺点是你无法记录每个参数的作用。

hope I'm not too late on this topic, but you can do something like this

/**
  * Get a page from the server
  * @param {PageRequest} pageRequest
  * @param {(pageResponse: PageResponse, pageRequestStatus: PageRequestStatus) => void} callback
  */
this.getPage = function(pageRequest, callback) {}

the downside of this approach is that you can't document what each of the arguments does.

花伊自在美 2024-09-14 22:04:02

@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) {
};

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

@link can add inline links to methods and classes.

/**
 * 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) {
};

Not ideal, but it gets the job done.

醉生梦死 2024-09-14 22:04:02

Google Closure 编译器注释具有类型表达式,用于这包括指示特定参数的类型、返回类型甚至 this 的能力。许多库正在考虑遵循 Google Closure Compiler Annotations,因为他们想用它来缩小代码。所以它有一些动力。缺点是我看不到给出描述的方法。

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

The Google Closure Compiler Annotations has Type Expressions for this which includes the ability to indicate type for specific arguments, return type, and even this. Many libraries are looking at following the Google Closure Compiler Annotations, because they want to use it to shrink their code. So it's got some momentum. The downside is I don't see a way to give the description.

For providing the description perhaps the JSDoc Toolkit Parameters With Properties approach would work (look at the bottom of the page). It's what I am doing right now. The JSDoc Toolkit is prepping to start work on V3, so feedback there might be good.

謸气贵蔟 2024-09-14 22:04:02

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

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

You could use @see to link to another method within the same class. The method would never be used, it would just be there for documentation purposes.

/**
 * @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 
};

If you are using some kind of build system, the dummy method could easily be omitted from the build.

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