How is this Javascript error interpreted?

0

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 ?

    
asked by Eduardo Sebastian 26.05.2017 в 20:00
source

3 answers

2

Leaving your code as it is without commenting return greet your variable greeting becomes a function like this:

var greeting = function () { return hello + name; };

This is because your function personalizedGreet() returns this same function, that we can see in this snippet.

 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");
    console.log(greeting);

Now if you comment the return your function personalizedGreet does not return anything, and your variable greeting is no longer a function as you would like it to be called in your 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");
    console.log(greeting);

It's just a variable that has no value assigned and returns that error.

    
answered by 26.05.2017 / 20:06
source
1

Since the function personalizedGreet(name) does not return anything, the value of greeting is undefined , so it gives you the error of greeting is not a function , since it is undefined - > undefined is not a function

In javascript if you do not specify the return value to a function return default undefined.personalizedGreet (name) does not return anything, so greeting is not a function, it is undefined,

    
answered by 26.05.2017 в 20:25
0

You yourself have answered, if you remove the return from the function PersonalGreet, it is simply a function that defines 2 variables and by not having a return when you assign it to greeting, there is no data to send to the alert ();

It is for this reason that you get the error in alert (greeting ());

    
answered by 26.05.2017 в 20:10