Created
February 22, 2015 18:24
-
-
Save jamc92/6f8925b4ac994e5f3556 to your computer and use it in GitHub Desktop.
JS - inArray prototype to a function
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>in_array in JS</title> | |
</head> | |
<body> | |
<script> | |
//Se asigna la funcion agregandole una propiedad de arreglo al objeto. | |
Array.prototype.inArray = function() { | |
//para la variable en este caso | |
for (var j in this) { | |
//Si la variable en su posicion es igual a los argumentos en su posicion cero, devuelve verdadero, sino, falso. | |
if (this[j] == arguments[0]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
//Se declara el arreglo y sus elementos | |
var frutas = ["Naranja", "Cambur", "Apple"]; | |
//Se valida el arreglo con la funcion en busqueda de un elemento interno | |
if (frutas.inArray("Naranja")) { | |
//Si lo tiene imprime la siguiente linea | |
document.write("Mi fruta favorita es la naranja"); | |
//Si no lo tiene imprime la siguiente linea | |
} else { | |
document.write("Si no hay naranja me quedo con la patilla"); | |
} | |
//en jQuery | |
$.inArray("Apple", frutas); | |
//do | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment