Created
July 12, 2011 08:12
-
-
Save Integralist/1077593 to your computer and use it in GitHub Desktop.
Cheap 'inArray' trick by @ded
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
/* | |
* jsFiddle: http://jsfiddle.net/integralist/fL2Xv/ | |
* Original: http://twitter.com/#!/ded/status/90531502097575936 | |
*/ | |
if (!Array.prototype.indexOf) { | |
Array.prototype.indexOf = function(item) { | |
return ( Math.abs( ~this.indexOf(item) ) )-1; | |
} | |
} |
Yep, I probably would do that in a production script, but for the purpose of example it just makes it clearer (in my opinion).
Not that [].indexOf
is more complicated or confusing in any way I just find the long winded route more appropriate for examples like this :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if (!Array.prototype.indexOf) {}
could be shortened toif (![].indexOf) {}
.