Created
December 3, 2020 22:37
-
-
Save ricardoalcocer/5b549a3a541bedbcc15fbfa4d432c047 to your computer and use it in GitHub Desktop.
JS Closure Snippet
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script lang="javascript"> | |
var module = function(){ | |
var theArray = []; | |
return{ | |
loader : function(){ | |
theArray[0] = function() { console.log("first function"); } | |
theArray[1] = function() { console.log("second function"); } | |
theArray[2] = function() { console.log("third function"); } | |
}, | |
getData : function(id){ | |
theArray[id](); | |
} | |
} | |
}(); | |
module.loader(); | |
module.getData(0); | |
module.getData(1); | |
module.getData(2); | |
</script> | |
</body> | |
</html> |
// lo pasé acá para que agarre el color coding
var function_Control = [];
function_Control[0] =function(param) {
console.log(param);
};
function_Control[1] =function(param) {
console.log(param);
};
function_Control[2] =function(param) {
console.log(param);
};
function_Control[3] =function(param) {
console.log(param);
};
function ejecuta(index, param) {
function_Control[index](param);
}
ejecuta(0, "parametro");
ejecuta(1, "Entretenido");
ejecuta(2, "Tranbajando");
ejecuta(3, "Entretenido");
ejecuta(0, "Nueva llmada ");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Estupendo. A mi me quedo de esta forma.
`var funcion_Control = [];
funcion_Control[0] =
function(param) {
funcion_Control[1] =
function(param) {
funcion_Control[2] =
function(param) {
funcion_Control[3] =
function(param) {
function ejecuta(index, param) {
}
ejecuta(0, "parametro");
ejecuta(1, "Entretenido");
ejecuta(2, "Tranbajando");
ejecuta(3, "Entretenido");
ejecuta(0, "Nueva llmada ");`