-
-
Save sebastien-p/1042874 to your computer and use it in GitHub Desktop.
Array.prototype.some
This file contains hidden or 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
[].some || (Array.prototype.some = function ( | |
a, // callback | |
b, // thisObject | |
c, // 'this' shortcut | |
d // placeholder for iterator/result | |
){ | |
for ( | |
c = this, | |
d = c.length; | |
d--; | |
) | |
if (d in c && a.call(b, c[d], d, c)) break; | |
return !!~d // d's value can be 0 to X (if callback returns true) or -1 | |
}) |
This file contains hidden or 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
[].some||(Array.prototype.some=function(a,b,c,d){for(c=this,d=c.length;d--;)if(d in c&&a.call(b,c[d],d,c))break;return!!~d}) |
This file contains hidden or 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Sebastien P. https://twitter.com/#!/_sebastienp | |
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. |
This file contains hidden or 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
{ | |
"name": "Array.prototype.some", | |
"description": "ES5 Array.prototype.some polyfill", | |
"keywords": [ | |
"ES5", | |
"Array", | |
"some", | |
"polyfill" | |
] | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>false</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
Array.prototype.some=function(a,b,c,d){for(c=this,d=c.length;d--;)if(d in c&&a.call(b,c[d],d,c))break;return!!~d} | |
document.getElementById("ret").innerHTML = ["140","bytes","rocks"].some(function(a){return a==="sucks"}) | |
</script> |
Is function(a,b){for(var c=this,d=-1,e=c.length>>>0;++d<e;)if(d in c&&a.call(b,c[d],d,c))break;return d<e} better ?
Yap better.
Though I don't think ES5 fallbacks are the best choice for code golf in general because things should be really tight and follow spec as these methods are added to native prototypes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jdalton : you said "it's more hacky than correct", you're right, but I think it's kinda the whole point of 140bytes.
Is
function(a,b){for(var c=this,d=-1,e=c.length>>>0;++d<e;)if(d in c&&a.call(b,c[d],d,c))break;return d<e}
better ?