Created
October 22, 2013 02:53
-
-
Save eamexicano/7094490 to your computer and use it in GitHub Desktop.
Ejemplo para extender la funcionalidad de los arreglos.
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
| Array.prototype.random = function () { | |
| var self = this | |
| var conteo = self.length | |
| var temp, x | |
| for (var i = conteo - 1; i >= 0; i--){ | |
| temp = self[i] | |
| x = Math.floor(Math.random() * conteo) | |
| self[i] = self[x] | |
| self[x] = temp | |
| } | |
| return self | |
| } | |
| /* | |
| Después de agregar la función shuffle en los arreglos se puede utilizar directamente | |
| var foo = [1,2,3,4,5,6] | |
| foo.random() | |
| */ | |
| Array.prototype.remove = function (element) { | |
| var index = this.indexOf(element) | |
| if (index != -1) { | |
| this.splice(index, 1) | |
| } | |
| return this | |
| } | |
| /* | |
| var test = [1,2,3,4,5] | |
| test.remove(3) | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment