Observer pattern in JavaScript