Skip to content

Instantly share code, notes, and snippets.

@takunagai
Last active August 29, 2015 14:02
Show Gist options
  • Save takunagai/76595108a85ae4d6883a to your computer and use it in GitHub Desktop.
Save takunagai/76595108a85ae4d6883a to your computer and use it in GitHub Desktop.
IE8以前で forEach が使えるようにする
/**
* forEachをサポートしていないIE8以前で forEach が使えるようにする
*
* Production steps of ECMA-262, Edition 5, 15.4.4.18
* Reference: http://es5.github.com/#x15.4.4.18
* https://developer.mozilla.org/ja/docs/JavaScript/Reference/Global_Objects/Array/forEach
* https://gist.github.com/oreo3/76595108a85ae4d6883a
*/
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this === null) {
throw new TypeError("this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0;
if ({}.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {T = thisArg;}
k = 0;
while(k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment