js refactoring to oop