I'm trying to understand the argument passing mechanism for function expressions that have functions within them, requiring argument passing. I think I know what is happening, but not sure.
I have the following code fragment:
makePassword(password) {
return function guess(passwordGuess) {
return (passwordGuess === password);
}
}
var tryGuess = makePassword("secret");
console.log("Guessing 'nope': " + tryGuess("nope"));
console.log("Guessing 'secret': " + tryGuess("secret"));
tryGuess
gets the function reference of makePassword("secret")
. I understand that. I'm trying to wrap my head around why tryGuess("nope")
passes nope
to the inner function guess
and not to makePassword
? I'm think that, in this example, password
is set before the function reference is passed to tryGuess
?
If that is so, how would I pass the parameters to password
and passwordGuess
in tryGuess
if I had assigned tryGuess
like this:
var tryGuess = makePassword();
There is an assumption about nested functions and parameter passing that I must be missing.