mozilla

Revision 455075 of Web terminology guide

  • Revision slug: Web/Guide/Terminology
  • Revision title: Web terminology guide
  • Revision id: 455075
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment new page

Revision Content

{{draft}}

This guide to common Web terminology will help you understand the rest of the documentation here on MDN a little better.

Polyfill

polyfill is a piece of code that implements a common API that isn't natively supported by a browser. This differs from a {{anch("shim")}}, which may fix or augment existing functionality but uses its own API to do so. By using a polyfill, you can often make code work even on older browsers without having to special case anything other than the addition of the polyfill.

Implementing a polyfill is usually done by looking to see if the API terms exist, and if they don't, adding them. For example:

if (!Object.create) {
  Object.create = (function() {
    function F() {}

    return function(o) {
      if (arguments.length != 1) {
        throw new Error("Object.create implementation only accepts one parameter.");
      }
      F.prototype = o;
      return new F();
    }
  })();
}

This code looks to see if the JavaScript Object.create() function exists, and creates it if it doesn't, thereby adding this functionality to older browsers.

Shim

shim is a piece of code used to correct the behavior of code that already exists, usually by adding new API that works around the problem. This differs from a {{anch("polyfill")}}, which implements a new API that is not supported by the stock browser as shipped.

Revision Source

<p>{{draft}}</p>
<p>This guide to common Web terminology will help you understand the rest of the documentation here on MDN a little better.</p>
<h2>Polyfill</h2>
<p>A&nbsp;<strong>polyfill</strong> is a piece of code that implements a common API that isn't natively supported by a browser.&nbsp;This differs from a&nbsp;{{anch("shim")}}, which may fix or augment existing functionality but uses its own API to do so. By using a polyfill, you can often make code work even on older browsers without having to special case anything other than the addition of the polyfill.</p>
<p>Implementing a polyfill is usually done by looking to see if the API terms exist, and if they don't, adding them. For example:</p>
<pre class="brush: js">
if (!Object.create) {
  Object.create = (function() {
    function F() {}

    return function(o) {
      if (arguments.length != 1) {
        throw new Error("Object.create implementation only accepts one parameter.");
      }
      F.prototype = o;
      return new F();
    }
  })();
}</pre>
<p>This code looks to see if the JavaScript <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create" title="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create"><code>Object.create()</code></a> function exists, and creates it if it doesn't, thereby adding this functionality to older browsers.</p>
<h2>Shim</h2>
<p>A&nbsp;<strong>shim</strong> is a piece of code used to correct the behavior of code that already exists, usually by adding new API that works around the problem. This differs from a {{anch("polyfill")}}, which implements a new API that is not supported by the stock browser as shipped.</p>
Revert to this revision