I was studying on JS Decorators and don't understand how the wrapper is able to access the inner functions argument. The snippet works, but I don't understand why the anonymous function 'function(val)' gets access to val, the argument of slow().
// https://codepen.io/lideebe/pen/VOjGvb
// simple function that gets wrapped
function slow(x){
return x * 3;
}
// the wrapper function
function cacheDecorator(func){
return function(val){ // How does this anonymous function get access to val?
return func(val)
}
}
// do the wrap
slow = cacheDecorator(slow);
// call the function
console.log(slow(2));
Output is 6 and that is correct.