PubSub

Since the dawn of JavaScript, browsers have allowed event handlers to be attached to DOM elements like so:

​ 
link.onclick = clickHandler;

Ah, simplicity itself! There’s just one caveat: if you wanted two click handlers for an element, you’d have to aggregate them yourself with a wrapper function.

​ 
link.onclick = ​function​() {
​ 
clickHandler1.apply(this, arguments);
​ 
clickHandler2.apply(this, arguments);
​ 
};

Not only is this tedious, it’s also a recipe for bloated, all-purpose handler functions. That’s why the W3C added addEventListener to the DOM specification in 2000 and jQuery abstracted it with the bind method. bind makes it easy to add as many handlers as you like to any event on any element (or set ...

Get Async JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.