8 steps to turn imperative JavaScript class to a functional declarative code
Published in
6 min readMar 6, 2018
As an example we will take JavaScript code to create event manager adding event listeners and dispatching events. Since JavaScript is a multiparadigm programming language, my intent is to show you options of how the worlds of imperative and declarative codes may be used together so you can make your own decision which features of the language you want to use.
To level up, please also consider my post Imperative vs Declarative programming in JavaScript or watching my YouTube video:
Our starting imperative code
Our imperative EventManager
is here:
class EventManager { construct (eventMap = new Map ()) {
this.eventMap = eventMap;
} addEventListener (event, handler) {
if (this.eventMap.has (event)) {
this.eventMap.set (event, this.eventMap.get (event).concat ([handler]));
} else {
this.eventMap.set (event, [handler]);
}
} dispatchEvent (event) {…