使用 jsdoc 记录匿名对象和函数的最佳方法
编辑:从技术上讲,这是一个由两部分组成的问题。我选择了涵盖一般问题的最佳答案,并链接到处理特定问题的答案。
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用 @name 标签记录代码中不存在的内容。
这是@name 标签文档。您可能会发现名称路径也很有用。
You can document stuff that doesnt exist in the code by using the @name tag.
Here is the @name tag documentation. You might find name paths useful too.
您可以使用
@callback
或@typedef
。You can use
@callback
or@typedef
.为了补充 studgeek 的回答,我提供了一个示例,展示了带有 Google Closure 的 JsDoc编译器可以让你做到。
请注意,记录的匿名类型将从生成的缩小文件中删除,并且编译器确保传入有效对象(如果可能)。但是,即使您不使用编译器,它也可以帮助下一个开发人员和 WebStorm (IntelliJ) 等工具理解它并为您提供代码补全。
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.
希望我在这个话题上还不算太晚,但是你可以做这样的事情,
这种方法的缺点是你无法记录每个参数的作用。
hope I'm not too late on this topic, but you can do something like this
the downside of this approach is that you can't document what each of the arguments does.
@link
可以添加方法和类的内联链接。不太理想,但它可以完成工作。
@link
can add inline links to methods and classes.Not ideal, but it gets the job done.
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.
您可以使用 @see 链接到同一类中的另一个方法。该方法永远不会被使用,它只是用于文档目的。
如果您使用某种构建系统,则可以轻松地从构建中省略虚拟方法。
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.
If you are using some kind of build system, the dummy method could easily be omitted from the build.