简体   繁体   English

如何为这样的对象定义 JSDoc?

[英]How to define JSDoc for such an object?

For example I have an object description via @name:例如,我通过@name 有一个对象描述:

/**
@name Point
@prop {number} x
@prop {number} y
*/

And an object, where each property is Point:还有一个对象,其中每个属性都是 Point:

/**
 * 
 * @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}
};

What type should it be?应该是什么类型? Is it possible to set the type only by property values, without keys?是否可以仅通过属性值设置类型,而无需键? Because I'm going to add/remove properties even on the fly, but all values will always be Point.因为我要即时添加/删除属性,但所有值将始终为 Point。

Is it possible to describe using jsDoc?是否可以使用 jsDoc 进行描述?

As I understand it there are 2 approaches to defining the keys and types of an object in JSDocs.据我了解,有两种方法可以在 JSDocs 中定义对象的键和类型。

JSDocs as defined by usejsdoc.com uses @property : JSDocs如下定义usejsdoc.com使用@property

/**
  @typedef PropertiesHash
  @type {object}
  @property {string} id - an ID.
  @property {string} name - your name.
  @property {number} age - your age.
 /

/** @type {PropertiesHash} /
var props;

Where as when used at Google, specifically with Google Closure prefers a {{key:(type)}} structure:当在 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;

To answer your question directly, it sounds like you don't know all of your keys, so you'll have to use a simpler definition.要直接回答您的问题,听起来您似乎并不知道所有密钥,因此您必须使用更简单的定义。

/** @type {Object<string, Point>} */

or shorthand;或速记;

/** @type {Object<Point>} */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM