我可以在 Google 闭包中为枚举定义接口吗?
Can I define an interface for an enum in Google Closure?
在 Google 闭包 javascript 中,我可以用
之类的东西为 class 定义接口
/**
* 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
};
有点不清楚你在说什么你是在寻找一个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()
};
在 Google 闭包 javascript 中,我可以用
之类的东西为 class 定义接口/**
* 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
};
有点不清楚你在说什么你是在寻找一个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()
};