HaS*_*Ste 5 javascript documentation jsdoc ecmascript-6
我需要用JSDoc记录一个ES6类,该类接收一个对象,该对象的属性将键名用作人员名称,因此键名几乎可以是任何字符串,而无需预定义。因此,对象的结构应如下所示:
{
"Name of person": {
"age": 31,
"hobby": "Tennis"
},
"Name of another person": {
"age": 29,
"hobby": "Running"
}
}
Run Code Online (Sandbox Code Playgroud)
因此,每个人的名字都是关键,但可以是任何东西,不是预先定义的。我要记录的示例:
class ExampleClass {
/**
* Creates an instance of ExampleClass
* @param {Object} peopleObj - Contains information about people.
* @param {String} peopleObj.name - The name of the person. <----- how should this be documented?
* @param {Number} peopleObj.name.age - The age of the person.
* @param {String} peopleObj.name.hobby - The hobby of the person.
* @memberof ExampleClass
*/
constructor(peopleObj) {
// Do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得如果我输入“ peopleObj.name”,则意味着密钥应该是“ name”,而不是您喜欢的任何名称。那么,如何让用户知道他可以在其中插入自己喜欢的任何名称来对此进行记录?
编辑
对于任何想知道的人,这就是我最终记录的方式(由于有人关闭了该问题,因此无法将其添加为答案)。
/**
* Information about a single person.
* @typedef Person
* @type {object}
* @property {number} age - The age of the person.
* @property {string} hobby - The hobby of the person.
*/
class ExampleClass {
/**
* Creates an instance of ExampleClass.
* @param {Object.<string, Person>} peopleObj - Information about people as {@link Person}
* objects where the key is their name.
* @memberof ExampleClass
*/
constructor(peopleObj) {
// Do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
@type JSDoc 文档中涵盖了您所描述的内容。
该对象应记录如下:
/**
* @typedef Person
* @type {Object}
* @property {number} age - the person's age
* @property {string} hobby - the person's hobby
*/
/**
* ExampleClass
*/
class ExampleClass {
/**
* Creates a dictionary of people
* @param {Object.<string, Person>} peopleObj - an object with names as keys and Person objects as values.
* @memberof ExampleClass
*/
constructor(peopleObj) {}
}
Run Code Online (Sandbox Code Playgroud)
为了以防万一,@typedef也支持它自己的类型。例如,@typedef {Object} Person或@typedef {{}} Person可以用来代替@type,因此被省略。
| 归档时间: |
|
| 查看次数: |
2111 次 |
| 最近记录: |