Last active
April 22, 2019 05:57
-
-
Save UskeS/a782c875e07a6c13f007b88b4d170502 to your computer and use it in GitHub Desktop.
ECMAScript 3に実装するArray.filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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