1

Where does a returned function go if not stored somewhere? Shouldn't it get appended to the global object/current outer-context? Here's an example:

var setup = function(){
    console.log("xyz");
    return function goBack(){
        console.log("It's actually abc");
    }
}

Now, on calling setup() in the global scope, "xyz" is being shown in the console, but the returning function, i.e goBack is not being appended in the global scope.

setup() //Outputs "xyz"

Now, on trying to call goBack, it's undefined in global scope:

goBack() //error: goBack not defined

Now I could access goBack using setUp()() or by storing the returned function from setup() into a global variable. But, shouldn't I be able to access goBack from the global scope once I execute setup() ? Because if I had stored setup() into a global variable, I would have access to goBack via that variable. But what happens if I don't use a variable to store the returned function from setup()? Where does goBack return to exactly? Thank you.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 3
    *"shouldn't I be able to access goBack from the global scope"* – Why would you think so? – deceze Jan 26 '19 at 21:26
  • No, when you give a function a name like that the name does not automatically become a symbol anywhere but inside the function itself. – Pointy Jan 26 '19 at 21:27
  • 1
    Do you know where the result goes when you `return {}` or `return 1` and not use the result of the call? – Bergi Jan 26 '19 at 21:27

5 Answers5

4

You're returning a function object, which is the same as any other kind of object, or any other sort of value. If you don't assign it to anything, it simply goes out of scope and will be garbage collected. Nothing happens with it.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

I would say that you could not execute the goBackfunction because of the lexical scope : https://stackoverflow.com/a/1047491/1836175 not closures : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures.

If you want to execute your goBackfunction because it is returned by setup() you simply need to store the return of the setup function. Javascript has First-class functions : https://developer.mozilla.org/en-US/docs/Glossary/First-class_Function

So the code to be able to execute the goBack function later would be :

var later = setup()
// do whatever you want and when needed 
later() // -> will call the goBack function

Hope that helps to clarify what it is goind on :).

PS : and so yes, if not stored the function will be garbage collected (see deceze response).

BenoitVasseur
  • 772
  • 5
  • 8
-1

If you don't store it in a variable it will be garbage collected when the setup function returns and you can't access it any more. On the other hand you can write your setup function like this:

var setup = function(){
    console.log("xyz");

    window.goBack = function(){
        console.log("It's actually abc");
    }
}

and have the goBack function available on the global scope.

then you would access it via window.goBack() or just goBack()

Jay
  • 841
  • 1
  • 8
  • 19
-2

Actually, you are returning an object, which can be stored within a variable and then ran, or, you can also run it directly.

    function a(){
    console.log("a");
    return function b(){
    console.log("b");
    };
    }
    console.log("this is calling just a");
    a();
    console.log("this is calling the a and the returned object");
    a()();
Zorak
  • 709
  • 7
  • 24
-2

Just to add to deceze's answer - adding to global scope is generally a Bad Thing, so the language doesn't do it unless you really want it to.

An example - suppose you have a large application with lots of component bits, and many of those component bits return a function when called. Now you have to make sure every single component uses a different name for their function, otherwise you could end up overwriting someone else's global, which would be bad. And if you're using a javascript library, you need to make sure that you're not using any of the same names as the library, because then you could break their code.

Keeping stuff in scope is super important, and storing stuff in global is a good way to introduce bugs that are really really hard to fix.

Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20