Factory Pattern in JavaScript