LoginSignup
11
7

More than 5 years have passed since last update.

ENUMの便利な使い方「その1」

Last updated at Posted at 2013-04-17

配列をハッシュ的に扱う方法
JSで書いてるけど言語は問わないはず。
C言語ではよく使う。
こうやって使うというサンプルなので動きません

item.js
var SLOT = {
 NONE : 0,
 EQUIP : 1,
 INVENTORY : 2,
 STORAGE : 3,
 _SIZEOF : 4,
};

var item_slot = new Array(SLOT._SIZEOF);
item_slot[SLOT.NONE] = new SlotNone();
item_slot[SLOT.EQUIP] = new SlotEquip();
item_slot[SLOT.INVENTORY] = new SlotInventory();
item_slot[SLOT.STORAGE] = new SlotStorage();

item_slot[SLOT.INVENTORY].add(new Item(1001001));
var items = item_slot[SLOT.INVENTORY].pickup([1001001]);
items.forEach(function(item){
    item_slot[SLOT.EQUIP].remove(item);
});
items.forEach(function(item){
    switch(item.slot_type){
    case SLOT.NONE:
        item_slot[SLOT.STORAGE].add(item);
        break;
    default:
        throw new Error("エラーです");
        break;
    }
});

その2があります

11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7