5 Minute JavaScript #19: Polyfills

The past few weeks were dedicated to the useful array methods. However these methods have been implemented in ECMAScript 5 and are therefore not available in older browsers such as IE8 and sometimes we have to support legacy browsers.

In JavaScript, we use polyfills to create behavior that is not implemented in browser. We can use polyfills to add every kind of behaviour to our application and a lot of websites provide these polyfills.

Basically what happens is that we first do a feature detection. Is the feature available in the browser? For example, does Array.prototype.map exist? Yes? That’s great! It means the feature has been implemented and is probably faster than every polyfill that we could come up with. Is it not available? Too bad, but we can create the Array.prototype.map method ourselves.

The Mozilla Developer Network has some of the most robust polyfills that are still fast in use. It also has an excellent documentation about the array methods. Here’s the page for the map functionality.

Using a polyfill doesn’t change the way the method is being called. If your browser does not support the map-method and you use a polyfill, you will still use it like this: [1, 2, 3].map(…); This is useful, because you don’t have to change syntax for different browsers.

Another solution is to use a framework that has implemented these functionalities. One of my favourites is underscore. This framework adds more useful methods than just the array methods to your function toolbelt.

framework adds more useful methods than just the array methods to your function toolbelt.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About Arvraepe