Building Polyfills
Web Platform APIs for the Present and Future
Brandon Satrom
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
If you play a game of word association with a room full of frontend developers, polyfill will no doubt receive mention from more than a few participants. This wordtraditionally a synonym for fallback , spackle , or patch was unfamiliar to many developers until just a few years ago, but has become a staple of conversations about modern and open web technologies. And while polyfilling has traditionally operated as a practice for delivering modern features to nonmodern browsers ( oldIE , meaning IE68, is increasingly a synonym for older browsers), the concept has expanded over the last few years to include the addition of new and experimental platform features to all browsers, including the newest and most cutting-edge.
Before our eyes, the practice of polyfilling is evolving from an adoption technique to a full-blown design pattern that allows developers to spackle their browsers with whatever semantics they wish. AngularJS, a very popular and fast-growing app framework, encourages this practice by enabling developers to use directives to add custom HTML tags and components to their applications.
Design by polyfill extends beyond frameworks as well. One such example is the Web Components effort, a set of specifications in the W3C designed to make the creation of custom semantics and encapsulated components a feature in the browser itself. With Web Components, if you want a component, you need only create one yourself, or grab one from a third party. Its a powerful idea, and if the concept takes off, it will unlock a new set of capabilities for web developers while adding new depth and richness to the applications we create.
Who This Book Is For
When it comes to the art of patching browser capabilities via polyfills, developers fall into two camps: those using polyfills to provide modern web platform capabilities in their sites, and those building polyfills for the broader web community. The book is primarily focused on the latter camp, and is intended to serve as an introduction to polyfilling, to provide a set of principles and practical guidelines for building reliable polyfills , and finally, to provide a road map for the future of polyfilling itself. However, even if youre a JavaScript developer not especially interested in building polyfills, theres a lot of useful content in this book in the realm of test-driven development (TDD), JavaScript build workflows, and performance optimizations.
At present, countless articles and book chapters exist that cover the subject of polyfilling. However, in nearly every case, these articles are targeted at polyfill consumers, not the intrepid developers who build these libraries. One notable exception is Addy Osmanis guide for polyfill developers, an introductory read that I highly recommend. Beyond Addys article, there are few resources, online or in print, that cover the topic of polyfills from the viewpoint of the developer. A lack of definitive resources for building these libraries means that we must strike out on our own, or slowly dig through other libraries for patterns and ideas.
My sincere hope is that this book will close the gap by serving as a field guide for polyfill developers. Well start with a cursory introduction to polyfilling on the web platform, and Ill share some principles and guidelines for building polyfills that developers can rely on. Once the guidelines have been established, well shift the discussion to an in-depth walk-through of building a real-life, reasonably complex polyfills.
Once Ive covered the ins and outs of building polyfills, the last two chapters will shift to focus on that emerging trend on the web platform: the forward polyfill , or prollyfill . This pattern, as Ive mentioned already, enables developers to test and work with emerging concepts, sometimes ahead of the browser vendors themselves. By the time were done, youll have all the tools you need to build reliable polyfills for the present, and the future, of the Web.
How This Book Is Organized
This book is organized into two parts. In the first part, we discuss the ins and outs of polyfill development, starting with the current landscape of polyfilling in , where Ill walk through a couple of cases in which we can improve the speed of our polyfill.
In Part II of this book, well turn our focus to the future of polyfilling, the prollyfill . In , well close out the book with another walk-through and build a prollyfill for a cutting-edge web platform specification. Once were done, you should be well equipped to jump into the present, and future, of polyfilling.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold
Shows commands or other text that should be typed literally by the user. Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This element signifies a tip or suggestion.
Note
This element signifies a general note.
Warning
This element indicates a warning or caution.
Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at http://bit.ly/kendo-ui and http://bit.ly/slacker-js.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: Building Polyfills by Brandon Satrom (OReilly). Copyright 2014 Brandon Satrom, 978-1-449-37073-2.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .