Skip to content

Instantly share code, notes, and snippets.

@davidsa
Created February 11, 2020 02:23
Show Gist options
  • Save davidsa/736e58d61cb6d1a870e7e77cce1c1ef6 to your computer and use it in GitHub Desktop.
Save davidsa/736e58d61cb6d1a870e7e77cce1c1ef6 to your computer and use it in GitHub Desktop.
closure examples
function factory(i) {
return function() {
i++;
console.log(i);
};
}
const contadorDesde5 = factory(5); // 1
contadorDesde5();
contadorDesde5();
contadorDesde5();
const contadorDesde13 = factory(13);
contadorDesde13();
function saludar(nombre) {
return function(mensaje) {
console.log(mensaje + " " + nombre);
};
}
const saludarDavid = saludar("David");
saludarDavid("Hello");
saludarDavid("Ciao");
saludarDavid("Au revoir");
const saludarDianita = saludar("Dianita");
saludarDianita("bebe");
function sumar3Numeros(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
function sumar3(a, b, c) {
return a + b + c;
}
const sumadorDe10 = sumar3Numeros(10);
const sumadorDe13 = sumadorDe10(3);
const sumadorDe15 = sumadorDe10(5);
const valor = sumadorDe13(1);
const valor2 = sumadorDe15(5);
valor;
valor2;
function log(label) {
return function(valor) {
console.log(label + ": " + valor);
};
}
const logError = log("Error");
logError("Me tire el codigo");
logError("No me pasaste un numero");
const logSuccess = log("Success");
logSuccess("Exito");
logSuccess("Subi el archivo a word");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment