Refactoring/javascript pattern