Skip to Main Content
Async JavaScript
book

Async JavaScript

by Trevor Burnham
November 2012
Intermediate to advanced content levelIntermediate to advanced
106 pages
2h 20m
English
Pragmatic Bookshelf
Content preview from Async JavaScript

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 of elements), without worrying ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern Asynchronous JavaScript

Modern Asynchronous JavaScript

Faraz K. Kelhini
Learn TypeScript 3 by Building Web Applications

Learn TypeScript 3 by Building Web Applications

Sebastien Dubois, Alexis Georges

Publisher Resources

ISBN: 9781941222782Errata