枚举中的Javascript枚举

时间:2010-07-29 13:17:27

标签: javascript enums

我在javascript中有以下“Enum”来表示我的申请状态:

var State = {
   STATE_A: 0,
   STATE_B: 1,
   STATE_C: 2
   //...
}

现在,我希望每个州都有一个“子国家”。例如,STATE_B可能位于STATE_B1STATE_B2 ...

构建这个的最佳方法是什么?我会以某种方式在State“enum”中嵌入“枚举”吗?感谢

如果有一个更好的方法来完全构建它(除了枚举)我都是耳朵。基本上我需要能够设置和检查我的应用程序的状态,并且每个状态都可以(但不是必需)附加一个可以设置和检查的子状态。如果解决方案允许我进行超过1级的嵌套深度,那就更好了。

5 个答案:

答案 0 :(得分:5)

如果需要,可以使用某种位字段:

var State = function() {
  // Note this must increment in powers of 2.
  var subStates = 8;
  var A = 1 << subStates;
  var B = 2 << subStates;
  var C = 4 << subStates;
  var D = 8 << subStates;

  return {

    // A
    // Note this must increment in powers of 2.
    STATE_A:  A,
    STATE_A1: A | 1,
    STATE_A2: A | 2,
    STATE_A3: A | 4,
    STATE_A4: A | 8,
    STATE_A5: A | 16

    // B
    STATE_B:  B,
    STATE_B1: B | 1,

    // C
    STATE_C: C,
    STATE_C1: C | 1,
    STATE_C2: C | 2,
    STATE_C3: C | 4,
    STATE_C4: C | 8,

    // D
    STATE_D: D
  };
}();

// Set a state.
var x = State.STATE_A1; // Same as State.STATE_A | State.STATE_A1

// Determine if x has root state of A?
if(x & State.STATE_A == State.STATE_A) {
   console.log("Root state is A.");
}
else {
   console.log("Nope, not root state A.");
}

// Determine if x has sub-state A1?
if(x & State.STATE_A1 == State.STATE_A1) {
   console.log("A with Substate 1");
}

因此前8位保留用于设置子状态。当然,只要根状态和子状态可以适合32位整数,你就可以增加它。如果您需要解释为什么/如何工作(按位运算符),请告诉我。

答案 1 :(得分:2)

你所做的并不是真正的恩赐。您正在使用本机Javascript对象,只需将它们视为枚举,当您想在Javascript中使用类似枚举的对象时,这是完全可以接受的。

要回答你的问题,是的,你可以完全嵌套对象:

var State = {
   STATE_A: 0,
   STATE_B:{
      SUBSTATE_1 : "active",
      SUBSTATE_2 : "inactive"
   },
   STATE_C: 2
   //...
}

然后您只需使用点表示法来设置这些值,例如

State.State_B.SUBSTATE_2 = "active"

答案 2 :(得分:0)

我想你想要写一些像

这样的东西
if (State.STATE_A === someState) { ... }

您可以在State对象中定义另一个图层,如

var State = {
  STATE_A : 0
  STATE_B : {
    B1 : 1,
    B2 : 2,
  }
};
...
if (State.STATE_B.B1 == someState){...}

修改:根据您对问题的评论,可能会出现另一种方法。

//Creates state objects from you json.
function createStates(json) {
  var result = {};
  for(var key in json) {
    result[key] = new State(json[key]);
  }
  return result;
}

//State class
function State(value) {
  //If the state value is an atomic type, we can do a simple comparison.
  if (typeof value !== "object") {
    this.value = value;
    this.check = function(comp){ return value === comp; };
  }
  // Or else we have more substates and need to check all substates
  else if (typeof value === "object") {
    this.value = createStates(value);
    for(var key in this.value) {
      //Allows to access StateA.SubStateA1. Could really mess things up :(
      this[key] = this.value[key];
    }
    this.check = function(comp){
      for(var key in this.value) {
        if (this.value[key].check(comp) === true){
          return true;
        }
      }
      return false;
    };
  }
};

现在您可以使用

调用所有内容
var stateJson = {
      STATE_A : 0,
      STATE_B : {
        B1 : 1,
        B2 : 2
      }
    };
var states = createStates(stateJson);
alert(states.stateA.check(0)); // Should give true
alert(states.STATE_B.B1.check(1)); // Same here
alert(states.STATE_B.check(1)); //And again because value is valid for one of the substates.

答案 3 :(得分:0)

由于JavaScript不支持运算符重载,因此无法使用==运算符直接测试子状态的相等性。最接近的是使用instanceof运算符来检查状态是否属于给定类型,例如:

// All these functions are empty because we only need the type and there is no data

function State() {
}

function State_A() {
}
State_A.prototype = new State();

function State_B() {
}
State_B.prototype = new State();

function State_B1() {
}
State_B1.prototype = new State_B();

function State_B2() {
}
State_B2.prototype = new State_B();

由于函数也是对象,您可以将嵌套权限添加到State函数中:

State.STATE_A = new State_A();
State.STATE_B = new State_B();
State.STATE_B.STATE_B1 = new State_B1();
State.STATE_B.STATE_B2 = new State_B2();

并检查其类型:

var myState = State.STATE_B1;
myState instanceof State    // true
myState instanceof State_A  // false
myState instanceof State_B  // true
myState instanceof State_B1 // true

答案 4 :(得分:0)

function State () {
  this.superState = null;
}

State.prototype = {
    constructor: State

  , mkSubState () {
      var subState = new State ();
      subState.superState = this;
      return subState;
    }

  , isSubStateOf (superState) {
      var state = this;
      while (state !== null) {
        if (this.superState === superState) {
          return true;
        }
        state = this.superState;
      }
      return false;
    }

  , isSuperStateOf (subState) {
      while (subState !== null) {
        if (subState.superState === this) {
          return true;
        }
        subState = subState.superState;
      }
      return false;
    }
};

var States = {};
States.A = new State ();
States.A1 = States.A.mkSubState ();
States.A2 = States.A1.mkSubState ();
States.B = new State ();
States.B1 = States.B.mkSubState ();
States.B2 = States.B1.mkSubState ();


States.B2.isSubStateOf (B);   // true
States.B2.isSubStateOf (B1);  // true

States.B2.isSubStateOf (B2);  // false
States.B2.isSubStateOf (A);   // false
States.B2.isSubStateOf (A1);  // false
States.B2.isSubStateOf (A2);  // false