Skip to content

Instantly share code, notes, and snippets.

@hufyhang
Created December 27, 2015 15:33
Show Gist options
  • Save hufyhang/c303ce1b80c7b6f8a73e to your computer and use it in GitHub Desktop.
Save hufyhang/c303ce1b80c7b6f8a73e to your computer and use it in GitHub Desktop.
JavaScript Array.prototype.forEach Polyfill
/*
* forEach Polyfill
*
* 2015-12-27
*
* By Feifei Hang, http://feifeihang.info
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
'use strict';
(function () {
if (!Array.prototype.forEach) {
Array.prototype.forEach = function forEach (callback, thisArg) {
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
var array = this;
thisArg = thisArg || this;
for (var i = 0, l = array.length; i !== l; ++i) {
callback.call(thisArg, array[i], i, array);
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment