javascript 如何在 JSDoc 中记录字典?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19513955/
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
How to document a dictionary in JSDoc?
提问by áxel Costas Pena
Having next example:
有下一个例子:
var CONF = {
locale: {
"en": {
name: "English",
lang: "en-US"
},
"es": {
name: "Spanish",
lang: "es-ES"
}
}
};
And knowing that what the locale property contains is a dictionary object, which comes from the database, how can I document its inner properties with JSDoc?
并且知道 locale 属性包含的是一个来自数据库的字典对象,我如何用 JSDoc 记录它的内部属性?
Currently I am thinking to typedeftype for my locale objects, then may I be able to set the localeproperty to simply an Array of my defined type? Is this the right way to do it?
目前我正在考虑typedef为我的语言环境对象输入,然后我可以将该locale属性设置为我定义的类型的简单数组吗?这是正确的方法吗?
回答by áxel Costas Pena
According to the JSDoc 3 docs:
根据JSDoc 3 文档:
Arrays and objects (type applications and record types)
An object with string keys and number values:
{Object.<string, number>}
数组和对象(类型应用程序和记录类型)
具有字符串键和数值的对象:
{Object.<string, number>}
So it would be:
所以它会是:
/** @type {{locales: Object.<string, {name: string, lang: string}>}} */
var CONF = {
locales: {
en: {
name: "English",
lang: "en-US"
},
es: {
name: "Spanish",
lang: "es-ES"
}
}
};
Cleaner, using @typedef
清洁剂,使用 @typedef
/**
* @typedef {{name: string, lang: string}} locale
*/
/**
* @type {{locales: Object.<string, locale>}}
*/
var CONF = {
locales: {
en: {
name: "English",
lang: "en-US"
},
es: {
name: "Spanish",
lang: "es-ES"
}
}
};
回答by Daniel Winterstein
As far as I can tell:
据我所知:
Using @typedefand @propertyto define a custom type is the "correct" way in JSDoc. But it is cumbersome to write and ugly to read (a cardinal sin in documentation).
使用@typedef和@property定义自定义类型是 JSDoc 中的“正确”方式。但是写起来很麻烦,读起来也很丑(文档中的一个大罪)。
The record type is much neater (note the double {{s):
记录类型要整洁得多(注意 double {{s):
/** {{
name:string,
lang:string
}} */

