Skip to content

Instantly share code, notes, and snippets.

@eliperelman
Forked from 140bytes/LICENSE.txt
Created June 21, 2011 03:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliperelman/1037201 to your computer and use it in GitHub Desktop.
Save eliperelman/1037201 to your computer and use it in GitHub Desktop.
Array.prototype.every polyfill for 140byt.es

Array.prototype.every polyfill for 140byt.es

Execute an expression for every element in an array and determine whether all elements make the expression evaluate truthy. This polyfill adds the every method to the array prototype for environments lower than ECMAScript 5.

Array.prototype.every = [].every || // Use the native every method if available, otherwise:
function (
a, // expression to test each element of the array against
b, // optionally change the 'this' context in the given expression
c, // placeholder iterator variable
d // placeholder variable (stores context of original array)
) {
for (c = 0, d = this; c < d.length; c++) // iterate over all of the array elements
if (!a.call(b, d[c], c, d)) // call the given expression, passing in context, value, index, and original array
return !1; // if any expression evaluates false, immediately return since 'every' is false
return !0 // otherwise return true since all expressions evaluated to true
}
Array.prototype.every=[].every||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(!a.call(b,d[c],c,d))return!1;return!0}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Eli Perelman <http://eliperelman.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "ArrayPrototypeEveryPolyfill",
"description": "Array every polyfill method for ECMAScript versions older than 5.",
"keywords": [
"Array",
"prototype",
"every",
"polyfill",
"ecmascript5"
]
}
<!DOCTYPE html>
<title>Array.prototype.every polyfill</title>
<script>
Array.prototype.every=[].every||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(!a.call(b,d[c],c,d))return!1;return!0}
var arr = [2,4,6,8,10,12,14,16];
var arr2 = [1,2,3,4,5,6,7,8];
// logs false, not all values are less than 10
console.log(arr.every(function(value, index, myArray) {
return value < 10;
});
// logs true, all values in arr are even and all values in arr2 are less than 10
console.log(arr.every(function(value, index, myArray) {
// this refers to arr2, but myArray is still arr
return value % 2 === 0 && this[index] < 10;
}, arr2);
</script>
@jdalton
Copy link

jdalton commented Aug 21, 2011

This is not an ES5 compatible fallback for Array#every.
Also browsers like Chrome have a bug where Array.prototype.every = [].every will cause Array#every to become enumerable.

For more information on what can be changed to make this method ES5 compliant and info on a critical bug, that this gist shares, please see my comments on a similar gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment