Having the code:
function personalizedGreet(name)
{
var greet = function () { return hello + name; };
var hello = "Hello "; // Variable local definida después de la función anidada
return greet;
}
//En otra parte del código...
var greeting = personalizedGreet("Oscar");
alert(greeting()); //Muestra en pantalla: Hello Oscar
It works correctly, but if I comment the return greet , it tells me "TypeError: greeting is not a function , why ?
What I understand is that when returning greet , variable greeting saves the internal function that is greet , but if I do not return it tells me that greeting it's not a function and my problem with this understanding is:
Being this my understanding I think that if I do not return it, the variable greeting would save the function personalizedGreet(name) and would still be a function only doing nothing of what greet () does, but then how does it work ?