Simple state machines in JavaScript