Skip to content

Instantly share code, notes, and snippets.

@rkeVader
Created March 12, 2019 12:31
Show Gist options
  • Save rkeVader/bea24b62f1a5ec5c45e276c08e732f90 to your computer and use it in GitHub Desktop.
Save rkeVader/bea24b62f1a5ec5c45e276c08e732f90 to your computer and use it in GitHub Desktop.
if (Array.prototype.forEach != undefined) {
// Array->forEach exists... no polyfill necessary
console.log("Array->forEach exists. No PolyFill necessary!");
} else {
// Array->forEach does not exist... polyfill necessary
console.log("Array->forEach does not exist. Resorting to PolyFill!");
Array.prototype.forEach = function(callback, thisArg) {
if (typeof(callback) !== 'function') {
throw new TypeError(callback + ' is not a function!');
}
var len = this.length;
for (var i = 0; i < len; i++) {
// callback goes here...
callback.call(thisArg, this[i], i, this);
}
}
} // Array->forEach should now exist one way or another...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment