作者:潘巧军_837 | 来源:互联网 | 2023-05-17 12:25
ImwritingmyownlibraryfortheprojectatworkforabrowserapplicationandIamhavingthesam
I'm writing my own library for the project at work for a browser application and I am having the same old problem deciding how to comment the code.
我正在为一个浏览器应用程序的项目编写我自己的库,在决定如何注释代码时也遇到了同样的问题。
I'm trying to follow the JsDoc syntax, but will probably continue the Google Closure Compiler way. I may end up using two @return and @returns tags in the documentation, just for portability sake (when I setup the auto-generation of the documentation).
我正在尝试遵循JsDoc语法,但是可能会继续使用谷歌闭包编译器。我可能会在文档中使用两个@return和@returns标记,这只是为了便于移植(当我设置文档的自动生成时)。
Now, the question, how do you document the return of a custom anonymous object from a function? For example:
现在的问题是,如何从函数中记录自定义匿名对象的返回?例如:
return {
username: 'username',
password: 'password',
enabled: true
};
JsDoc has an example of how a @param can be documented to expect object with certain fields, but not the @returns tag. Similarly, the Google Closure Compiler documentation of a Record Type is vague and has no example to work it out.
JsDoc提供了一个例子,说明如何将@param文档化以期望具有特定字段的对象,而不是@returns标记。类似地,记录类型的谷歌闭包编译器文档是模糊的,没有示例来解决它。
3 个解决方案
13
The Closure-compiler uses a subset of the JSDoc annotations (and adds a few of its own). See the annotation reference for the compiler for the complete set. A JSDoc annotation is similar to a JavaDoc annotation and is a comment block that begins with /**
(two stars). While each line of the comment often begins with it's own *
, that is a convention that is not required. Only one JSDoc tag is allowed per line, but the arguments for a tag can span multiple lines.
Closure-compiler使用JSDoc注释的一个子集(并添加一些自己的注释)。查看完整集的编译器的注释引用。一个JSDoc注释类似于JavaDoc注释,是一个以/**(两颗星)开头的注释块。尽管注释的每一行通常以它自己的*开头,但这是一个不需要的约定。每行只允许有一个JSDoc标记,但是标记的参数可以跨多行。
The annotation typically applies to the following statement. Here are some examples:
注释通常应用于以下语句。下面是一些例子:
Variable
/** @type {string} */ var a;
Type Cast
var b = /** @type {string} */ (window['foo']);
note the extra parenthesis
注意额外的括号
Named Function
/**
* @param {string} bar
* @return {boolean}
*/
function foo(bar) { return true; }
Function Expressions
/** @type {function(string):boolean} */
var foo = function(bar) { return true; }
var foo2 =
/**
* @param {string} bar
* @return {boolean}
*/
function(bar) { return true; }
Typedef
Complex types (including unions, and record types) can be aliased for convenience and maintainability using a typedef. These annotations can be long, but can be split over multiple lines for readability.
使用typedef可以为方便和可维护性提供复杂类型(包括工会和记录类型)。这些注释可以很长,但是为了可读性,可以在多行中进行分割。
/** @typedef {{
* foo:string,
* bar:number,
* foobar:number|string
* }}
*/
var mytype;
For your original example, there are several possible ways to annotate such a function return value. One of the most specific and still convenient is the record type:
对于您的原始示例,有几种可能的方法来注释该函数的返回值。其中最具体也是最方便的是记录类型:
/** @return {{username:string, password:string, enabled:boolean}} */
function() {
return {
username: 'username',
password: 'password',
enabled: true
}
}
Note the extra {}
. Also keep in mind that record types will not prevent property renaming.
注意额外的{ }。还要记住,记录类型不会阻止属性重命名。
This annotation tells the compiler that the function returns an anonymous type with username
, password
and enabled
properties. Other valid options would be to define an interface elsewhere and typecast the return value to be that interface. The least specific annotation would be Object
or *
.
该注释告诉编译器该函数返回带有用户名、密码和启用属性的匿名类型。其他有效的选项是在其他地方定义接口,并将返回值类型转换为该接口。最不特定的注释将是Object或*。
To see a wide range of possible annotations, take a look at the extern files in the Compiler project.
要查看大量可能的注释,请查看编译器项目中的外部文件。