refactor js code to oo