Get started with es5-shim CDN

MIT licensed

The ES5-shim library provides polyfills for ES5 features in older browsers, enabling their usage without upgrading to a newer version.

Tags:
  • shim
  • es5
  • es5 shim
  • javascript
  • ecmascript
  • polyfill

Stable version

Copied!

How to start using es5-shim CDN


<!DOCTYPE html>
<html>
<head>
  <title>Get started with es5-shim CDN - cdnhub.io</title>
  <script src="https://cdn.cdnhub.io/es5-shim/4.6.7/es5-shim.min.js"></script>
</head>
<body>
  <script>
    // ES5-compatible version of 'bind' method for older browsers
    if (!Function.prototype.bind) {
      Function.prototype.bind = function(context_this) {
        if (typeof this !== "function") {
          throw new TypeError("Function.prototype.bind - What is trying to be bound is not a function");
        }

        var aArgs = Array.slice(arguments, 1),
            fToBind = this,
            myNewBinding = function() {
              return fToBind.apply(this instanceof fToBind ? this : context_this, aArgs.concat(Array.slice(arguments)));
            };

        if (this.prototype && this.prototype.constructor === fToBind) {
          myNewBinding.prototype = Object.create(this.prototype);
          myNewBinding.prototype.constructor = myNewBinding;
        }

        return myNewBinding;
      };
    }

    // ES5-compatible version of 'map' method for older browsers
    if (!Array.prototype.map) {
      Array.prototype.map = function(callback, thisArg) {
        var T, A, i, len, results;

        if (this === null) {
          throw new TypeError("Array.prototype.map called on null or non-object");
        }

        if (typeof callback !== "function") {
          throw new TypeError(callback + " is not a function");
        }

        len = this.length >>> 0;
        results = new Array(len);

        for (i = 0; i < len; i++) {
          if (i in this) {
            T = this[i];
            results[i] = callback(T, i, this);
          }
        }

        return results;
      };
    }

    // ES5-compatible version of 'filter' method for older browsers
    if (!Array.prototype.filter) {
      Array.prototype.filter = function(callback, thisArg) {
        var T, currentResult, index, length, results;

        if (this === null) {
          throw new TypeError("Array.prototype.filter called on null or non-object");
        }

        if (typeof callback !== "function") {
          throw new TypeError(callback + " is not a function");
        }

        length = this.length >>> 0;
        results = new Array(length);
        currentResult = undefined;

        for (index = 0; index < length; index++) {
          if (this[index] !== undefined) {
            T = this[index];
            currentResult = callback(T, index, this);

            if (currentResult !== undefined) {
              results[index] = T;
            }
          }
        }

        return results;
      };
    }

    // ES5-compatible version of 'forEach' method for older browsers
    if (!Array.prototype.forEach) {
      Array.prototype.forEach = function(callback, thisArg) {
        var T, i, length;

        if (this === null) {
          throw new TypeError("Array.prototype.forEach called on null or non-object");
        }

        if (typeof callback !== "function") {
          throw new TypeError(callback + " is not a function");
        }

        length = this.length >>> 0;

        for (i = 0; i < length; i++) {
          if (i in this) {
            T = this[i];
            callback(T, i, this);
          }
        }
      };
    }

    // Usage example
    var numbers = [1, 2, 3, 4, 5];
    var squares = numbers.map(function(num) {
      return num * num;
    });

    console.log(squares); // [1, 4, 9, 16, 25]
  </script>
</body>
</html>
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!

All versions