I've searched SO for this question and can't seem to find it: how does passing an argument in a called function directly to a nested function work? For example, take the following code, from "Eloquent Javascript:"
var dayName = function() {
var names = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
return function(number) {
return names[number];
};
}();
console.log(dayName(3));
// → Wednesday
The argument 3
is passed to dayName()
, but dayName()
does not accept any parameters directly. How does the argument get passed to the nested returned function? How would this differ is the nested function wasn't returned itself, but instead returned a value?
Lastly, consider this pseudo-code, where two arguments are passed to the dayName()
function, and both the dayName()
function and its nested function accept params:
var dayName = function(param) {
console.log(param);
(function(otherParam) {
console.log(otherParam);
});
};
dayName(outerFunctionParam, innerFunctionParam);
What is the proper syntax to pass one param to the dayName()
function and the second param to the nested function, and how does it work behind the scenes? Thanks!