我可以在Google Closure中为枚举定义一个接口吗?

时间:2015-01-21 17:44:41

标签: google-closure

在Google Closure javascript中,我可以使用类似

的类来定义类的接口
/**
 * An object that implements this does fantastic, wonderful things. 
 * @interface
 */
name.space.for.project.SomeInterface = {};

/**
 * Some method that does something.
 * @param {number} foo
 * @return {string} Some fantastic, wonderful string.
 */
name.space.for.project.SomeInterface.prototype.doSomething =
    function(foo) {};

我可以以某种方式为枚举声明一个接口,但不实现枚举吗?然后像往常一样在单独的文件中实现枚举?喜欢这个?

/**
 * Enumerates event types.
 * @enum {string}
 * @implements {name.space.for.project.SomeEnum}
 */
name.space.for.project.SomeEnum = {
    FOO: 1,
    BAR: 2
};

1 个答案:

答案 0 :(得分:1)

有点不清楚你在说什么你正在寻找一个Java风格的枚举,其中枚举是对象?

这是可能的。

/**
 * An object that implements this does fantastic, wonderful things. 
 * @interface
 */
name.space.for.project.SomeInterface = {};

/**
 * Some method that does something.
 * @param {number} foo
 * @return {string} Some fantastic, wonderful string.
 */
name.space.for.project.SomeInterface.prototype.doSomething =
    function(foo) {};

/**
 * Enumerates event types.
 * @enum {name.space.for.project.SomeInterface}
 */
name.space.for.project.SomeEnum = {
    FOO: new name.space.for.project.SomeInterface(),
    BAR: new name.space.for.project.SomeInterface()
};