Created
July 28, 2016 12:33
-
-
Save Sylvance/ab02d5a85649697c7874379d7b057f74 to your computer and use it in GitHub Desktop.
https://repl.it/Cgt4/0 created by Sylvance
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
var contains = function(needle) { | |
// Per spec, the way to identify NaN is that it is not equal to itself | |
var findNaN = needle !== needle; | |
var indexOf; | |
if(!findNaN && typeof Array.prototype.indexOf === 'function') { | |
indexOf = Array.prototype.indexOf; | |
} else { | |
indexOf = function(needle) { | |
var i = -1, index = -1; | |
for(i = 0; i < this.length; i++) { | |
var item = this[i]; | |
if((findNaN && item !== item) || item === needle) { | |
index = i; | |
break; | |
} | |
} | |
return index; | |
}; | |
} | |
return indexOf.call(this, needle) > -1; | |
}; | |
var myArray = [0,1,2], | |
needle = 1, | |
index = contains.call(myArray, needle); // true | |
console.log(index); |
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
Native Browser JavaScript | |
true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment