Javascript 如何在 JSDoc 中将对象数组指定为参数或返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14611995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:23:10  来源:igfitidea点击:

How to specify an array of objects as a parameter or return value in JSDoc?

javascriptdocumentationjsdoc

提问by Ray

In JSDoc, the best documentation I can find shows to use the following if you have an array of a specific type (such as an array of strings) as:

在 JSDoc 中,如果您有一个特定类型的数组(例如字符串数组),我能找到的最好的文档显示使用以下内容:

/**
 * @param {Array.<string>} myStrings All my awesome strings
 */
 function blah(myStrings){
     //stuff here...
 }

How would you replace the below question marks specify an array of objects?

你将如何替换下面的问号指定一个对象数组?

/**
 * @param {???????} myObjects All of my equally awesome objects
 */
 function blah(myObjects){
     //stuff here...
 }

回答by Rene Saarsoo

You should be more specific what you mean by JSDoc - this is a generic term covering pretty much all the JavaDoc-style documentation tools for JavaScript.

您应该更具体地说明 JSDoc 的含义——这是一个通用术语,涵盖了几乎所有 JavaScript 的 JavaDoc 风格的文档工具。

The syntax you used for array of strings looks like the one supported by Google Closure Compiler.

您用于字符串数组的语法类似于Google Closure Compiler支持的语法。

Using this, an array of Objects would be:

使用它,一个对象数组将是:

/**
 * @param {Array.<Object>} myObjects
 */

Or just an array of anything - this should work with pretty much all doc tools:

或者只是任何东西的数组 - 这应该适用于几乎所有的文档工具:

/**
 * @param {Array} myArray
 */

jsdoc-toolkit, JSDoc 3, and JSDucksupport the following syntax to denote an array of objects:

jsdoc-toolkitJSDoc 3JSDuck支持以下语法来表示对象数组:

/**
 * @param {Object[]} myArray
 */


EDIT

编辑

In case you know the keys and the variable type of the values you can also do:

如果您知道值的键和变量类型,您还可以执行以下操作:

/**
 * @param {Array.<{myNumber: Number, myString: String, myArray: Array}>} myObjects
 */

or

或者

/**
 * @param {{myNumber: Number, myString: String, myArray: Array}[]} myObjects
 */