如何为这样的对象定义 JSDoc?
How to define JSDoc for such an object?
例如我有一个对象描述通过@name:
/**
@name Point
@prop {number} x
@prop {number} y
*/
和一个对象,其中每个 属性 是点:
/**
*
* @type {what?}
*/
var details = {
something: {x:1.1, y: 2.2},
another: {x:1.1, y: 2.2},
theRest: {x:1.1, y: 2.2},
more: {x:1.1, y: 2.2},
yetAnother: {x:1.1, y: 2.2}
};
应该是什么类型的?是否可以仅通过 属性 值设置类型,而无需键?因为我要 add/remove 属性,即使是在运行中,但所有值将始终是 Point。
是否可以使用 jsDoc 进行描述?
据我所知,有两种方法可以在 JSDocs 中定义对象的键和类型。
usejsdoc.com 定义的 JSDocs 使用 @property
:
/**
@typedef PropertiesHash
@type {object}
@property {string} id - an ID.
@property {string} name - your name.
@property {number} age - your age.
/
/** @type {PropertiesHash} /
var props;
在 Google 中使用时,特别是 Google Closure 更喜欢 {{key:(type)}}
结构:
/**
* A typedef to represent a CSS3 transition property. Duration and delay
* are both in seconds. Timing is CSS3 timing function string, such as
* 'easein', 'linear'.
*
* Alternatively, specifying string in the form of '[property] [duration]
* [timing] [delay]' as specified in CSS3 transition is fine too.
*
* @typedef { {
* property: string,
* duration: number,
* timing: string,
* delay: number
* } | string }
*/
goog.style.transition.Css3Property;
要直接回答您的问题,听起来您并不了解所有密钥,因此您必须使用更简单的定义。
/** @type {Object<string, Point>} */
或shorthand;
/** @type {Object<Point>} */
例如我有一个对象描述通过@name:
/**
@name Point
@prop {number} x
@prop {number} y
*/
和一个对象,其中每个 属性 是点:
/**
*
* @type {what?}
*/
var details = {
something: {x:1.1, y: 2.2},
another: {x:1.1, y: 2.2},
theRest: {x:1.1, y: 2.2},
more: {x:1.1, y: 2.2},
yetAnother: {x:1.1, y: 2.2}
};
应该是什么类型的?是否可以仅通过 属性 值设置类型,而无需键?因为我要 add/remove 属性,即使是在运行中,但所有值将始终是 Point。
是否可以使用 jsDoc 进行描述?
据我所知,有两种方法可以在 JSDocs 中定义对象的键和类型。
usejsdoc.com 定义的 JSDocs 使用 @property
:
/**
@typedef PropertiesHash
@type {object}
@property {string} id - an ID.
@property {string} name - your name.
@property {number} age - your age.
/
/** @type {PropertiesHash} /
var props;
在 Google 中使用时,特别是 Google Closure 更喜欢 {{key:(type)}}
结构:
/**
* A typedef to represent a CSS3 transition property. Duration and delay
* are both in seconds. Timing is CSS3 timing function string, such as
* 'easein', 'linear'.
*
* Alternatively, specifying string in the form of '[property] [duration]
* [timing] [delay]' as specified in CSS3 transition is fine too.
*
* @typedef { {
* property: string,
* duration: number,
* timing: string,
* delay: number
* } | string }
*/
goog.style.transition.Css3Property;
要直接回答您的问题,听起来您并不了解所有密钥,因此您必须使用更简单的定义。
/** @type {Object<string, Point>} */
或shorthand;
/** @type {Object<Point>} */