@typedef¶
-
.. @typedef::
¶ - 语法
@typedef [<type>] <namepath>
- 概述
The @typedef tag is useful for documenting custom types, particularly if you wish to refer to them repeatedly. These types can then be used within other tags expecting a type, such as [@type]
type
or [@param]param
.Use the [@callback]
callback
tag to document the type of callback functions.- 示例
此示例为参数定义联合类型, 该参数可以包含表示数字的数字或字符串.
Using the @typedef tag¶/** * A number, or a string containing a number. * @typedef {(number|string)} NumberLike */ /** * Set the magic number. * @param {NumberLike} x - The magic number. */ function setMagicNumber(x) { }
此示例定义了一个更复杂的类型, 一个具有多个属性的对象, 并设置其名称路径, 以便它与使用该类型的类一起显示. 因为类实际上没有公开类型定义, 所以习惯上将类型定义记录为内部成员.
Using @typedef to document a complex type for a class¶/** * The complete Triforce, or one or more components of the Triforce. * @typedef {Object} WishGranter~Triforce * @property {boolean} hasCourage - Indicates whether the Courage component is present. * @property {boolean} hasPower - Indicates whether the Power component is present. * @property {boolean} hasWisdom - Indicates whether the Wisdom component is present. */ /** * A class for granting wishes, powered by the Triforce. * @class * @param {...WishGranter~Triforce} triforce - One to three {@link WishGranter~Triforce} objects * containing all three components of the Triforce. */ function WishGranter(triforce) {}