+ 5
Help me understand the return statement in the nested function.
function showName(firstName,lastName) { var nameIntro = "This is "; function displayName() { return nameIntro + firstName + " " + lastName; } return displayName(); }
4 ответов
+ 4
what if I just remove the return displayName() and use displayName();
+ 4
Thanks, that was helpful..
+ 2
Here, the inner function displayName is defined within the function showName, but it is not called until you get to the return displayName(); statement. At which point the inner function returns (nameIntro + firstName + " " + lastName;) to the outer functions return statement which will then return to where the outer function showName was called.
+ 2
function showName(firstName, lastName) {
return "This is " + firstName + " " + lastName;
}
This is essentially the same thing. Provided there isn't a call somewhere else to the inner function itself. Similar to:
showName(firstName, lastName).displayName();