@interface

.. @interface::
语法

@interface [<name>]

概述

使用JSDoc标记字典(默认启用):

With the Closure Compiler tag dictionary:

The @interface tag marks a symbol as an interface that other symbols can implement. For example, your code might define a parent class whose methods and properties are stubbed out. You can add the @interface tag to the parent class to indicate that child classes must implement the parent class’ methods and properties.

Add the @interface tag to the top-level symbol for the interface (for example, a constructor function). You do not need to add the @interface tag to each member of the interface (for example, the interface’s instance methods).

如果您使用的是JSDoc标记字典(默认情况下已启用), 您还可以使用虚拟注释定义接口, 而不是通过编写接口的代码. 有关示例, 请参阅“`定义接口的虚拟注释<#virtual-comments>`__”.

示例

在下面的示例中, ``Color``函数表示其他类可以实现的接口:

Using the @interface tag
/**
 * Interface for classes that represent a color.
 *
 * @interface
 */
function Color() {}

/**
 * Get the color as an array of red, green, and blue values, represented as
 * decimal numbers between 0 and 1.
 *
 * @returns {Array&lt;number>} An array containing the red, green, and blue values,
 * in that order.
 */
Color.prototype.rgb = function() {
    throw new Error('not implemented');
};

以下示例使用虚拟注释而不是代码来定义“Color”接口:

Virtual comments that define an interface
/**
 * Interface for classes that represent a color.
 *
 * @interface Color
 */

/**
 * Get the color as an array of red, green, and blue values, represented as
 * decimal numbers between 0 and 1.
 *
 * @function
 * @name Color#rgb
 * @returns {Array&lt;number>} An array containing the red, green, and blue values,
 * in that order.
 */