learning state design patterns in js