What is the difference between a shim and a polyfill?

Viewed 96640

Both seem to be used in web development circles, see e.g. HTML5 Cross Browser Polyfills, which says:

So here we're collecting all the shims, fallbacks, and polyfills...

Or, there's the es5-shim project.

In my current project we're using a number of these, and I want to stick them all in the same directory. So, what should I call this directory---shims, or polyfills?

6 Answers

Polyfill is just a script that will check a certain API existence in the browser, ok? If the API doesn't exist in the browser polyfill(which is a simple script) will act something like this:

for example, I'm gonna use Bluetooth API in the browser and I know some browser doesn't have such API so I will write something like this in order to check my API existence:

if(!navigator.bluetooth) { // write polyfill here } 

Shim is a script as well which mostly provided as a plugin or library and how it works?

Actually, it will override an already existing API and implements a different behavior in order to support new APIs in older browsers. It worth noting that Shims mostly are used for backward compatibility.

Related