Skip to content

Instantly share code, notes, and snippets.

@UskeS
Last active April 22, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UskeS/a782c875e07a6c13f007b88b4d170502 to your computer and use it in GitHub Desktop.
Save UskeS/a782c875e07a6c13f007b88b4d170502 to your computer and use it in GitHub Desktop.
ECMAScript 3に実装するArray.filter
// Production steps of ECMA-262, Edition 5.1, 15.4.4.20
// https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.20
// from http://javascript.boxsheep.com/polyfills/Array-prototype-filter/
if (!Array.prototype.filter) {
Array.prototype.filter = function(callbackfn , thisArg) {
"use strict";
var O = Object(this),
lenValue = O.length,
len = lenValue >>> 0,
T,
A,
k,
to,
Pk,
kPresent,
kValue,
selected;
if (typeof callbackfn !== "function") {
throw new TypeError();
}
T = thisArg ? thisArg : undefined;
A = new Array();
k = 0;
to = 0;
while(k < len) {
Pk = k.toString();
kPresent = O.hasOwnProperty(Pk);
if (kPresent) {
kValue = O[Pk];
selected = callbackfn.call(T, kValue, k, O);
if (!!selected) {
A.push(kValue);
to += 1;
}
}
k += 1;
}
return A;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment