I have the following code:
function creaSumador(x) {
return function(y) {
return x + y;
};
}
var suma5 = creaSumador(5);
var suma10 = creaSumador(10);
console.log(suma5(2)); // muestra 7
console.log(suma10(2)); // muestra 12
I can do it myself
function suma(x,y) {
return x + y;
}
console.log(suma(5,2)); // muestra 7
console.log(suma(10,2)); // muestra 12
I do not see much importance to the functions clousures
, since it can also be done without clousures
, can you explain me when to use clousure
, when in fact it is necessary?