我想在我定义的某些枚举之间继承。
假设我有以下代码:
/**
* @enum {!number}
*/
var MyBaseEnum = {
IS_VALID : 0b00000001,
UNDER_VALIDATION : 0b00000010,
SUBMITTING : 0b00000100
};
/**
* @enum {!number}
* @extends {MyBaseEnum}
*/
var MyChildEnum = {
AN_ODDBALL_STATE : 0b00001000,
ANOTHER_ODDBALL_STATE : 0b00010000
};
goog.inherits(MyChildEnum, MyBaseEnum);
/**
* @param {!MyBaseEnum} myEnum
* @return {!string}
*/
function iExpectBaseEnum (myEnum) {
switch (myEnum) {
case MyBaseEnum.IS_VALID:
return "It's valid, man!";
case MyBaseEnum.UNDER_VALIDATION:
return "Chill, I'm working on it.";
case MyBaseEnum.SUBMITTING:
return "Under submission, bro.";
default:
return 'WAT';
}
};
/**
* @param {!MyChildEnum} myEnum
* @return {!string}
*/
function iExpectChildEnum (myEnum) {
switch (myEnum) {
case MyChildEnum.AN_ODDBALL_STATE:
return 'Dunno.';
case MyChildEnum.ANOTHER_ODDBALL_STATE:
return 'Dunno, bro.';
// Accessing parent's IS_VALID would be nice through MyChildEnum.
case MyChildEnum.IS_VALID:
return "It's valid, man!";
// But also would be nice to have it work with paren'ts value.
case MyBaseEnum.UNDER_VALIDATION:
return "Chill, I'm working on it.";
case MyBaseEnum.SUBMITTING:
return "Under submission, bro.";
default:
return 'WAT';
}
};
此刻,我只是无法找到在枚举之间进行继承的方法并且仍然让它们的行为像枚举一样(我可以用实现我自己的枚举系统的方法来做一个难看的臭臭黑客泛型,但是...糟糕... +我会从编译器中损失很多汁液。
在枚举之间是否有任何继承关系或以某种方式解决了枚举专用性和扩展性的概念性问题?
答案 0 :(得分:0)
我认为,使用Google Closure Compiler不能在枚举之间继承。
我的假设基础是Java不允许枚举继承另一个枚举(尽管使用接口存在一些麻烦),并且Google Closure Compiler似乎受到Java的启发。