function StateMachine states this states states this indexes for var t

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function StateMachine(states) {
this.states = states;
this.indexes = {};
for(var i = 0; i < this.states.length; ++i){
this.indexes[this.states[i].name] = i;
if (this.states[i].initial){
this.currentState = this.states[i];
}
}
this.consumeEvent = function(e){
if(this.currentState.events[e]){
this.currentState = this.states[this.indexes[this.currentState.events[e]]];
}
}
this.getStatus = function(){
return this.currentState.name;
}
}