Last active
May 31, 2025 18:17
-
-
Save Integralist/1077593 to your computer and use it in GitHub Desktop.
Cheap 'inArray' trick by @ded #js
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
/* | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :-)