So i came across some closure examples, and i would like to know how does this work. I solved it by mistake and after a few hours of trying. but i am still trying to understand how the values are passed. I understand that once i create a local variable (func) the value associated with it doesnt really change, so i can keep referencing that initial value. My question is how when i return the inner function i am able to transfer the values in logArea to the callback function. can someone explain like step by step?
const wrapLog = function(callback, name) {
let func = callback;
return function(...param) {
return func(...param);
}
};
const area = function(x, y) {
return x * y;
};
const logArea = wrapLog(area, "area");
console.log(logArea(5, 3)); // area(5, 3) => 15
console.log(logArea(3, 2)); // area(3, 2) => 6
const volume = function(x, y, z) {
return x * y * z;
};
const logVolume = wrapLog(volume, "volume");
console.log(logVolume(5, 3, 2)); // volume(5, 3, 2) => 30
console.log(logVolume(3, 2, 4)); // volume(3, 2, 4) => 24