2

Local variables in functions seem to persist after the function is called. Is this always the case? How does this work?

    // example 1
    function Obj(str) {
        this.show = function () { return str; };
    }

    var o1= new Obj("jeff");
    var o2 = new Obj("eric");

    o1.show();  // => "jeff"  (unexpected?)
    o2.show();  // => "eric"

The same thing happens here:

    // example 2
    function newObj(str) {
        return { show: function () { return str; } };
    }

    var o3 = newObj("jeff");
    var o4 = newObj("eric");

    o3.show();  // => "jeff"  (unexpected?)
    o4.show();  // => "eric"

But what about in this case?

    // example 3
    function rand() {
        var a = Math.random(0);
        return a;
    }

    for (var i = 0; i < 1000000; i++) rand();   // are there a million random numbers stored somewhere?
StormDog
  • 91
  • 6
  • 4
    both first examples create objects and store them into a container `oX`, the last one does nothing. – Hacketo Sep 09 '15 at 14:25
  • 2
    Maybe review this http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Larry Turtis Sep 09 '15 at 14:26
  • @Hacketo, yes I agree, but I don't think it depends on creating objects. Please see my (possible) answer below. – StormDog Sep 09 '15 at 16:35
  • @Larry Turtis, Thanks for the link. I didn't see anything in there about the life of variables, but I might have missed it. I proposed an answer to my question below. – StormDog Sep 09 '15 at 16:36
  • @StormDogI this link about scope is the life of variables. If a variable exists in a scope they still exist in that scope. So when `rand` is executed as there is no scope that use `a` , `a` is destroyed at the end of the function call. – Hacketo Sep 09 '15 at 18:07
  • @StormDog the constructor of the first object define a scope that save `str` and the function `show` use that variable so when you store this object in a container, `str` still exists. – Hacketo Sep 09 '15 at 18:16

2 Answers2

0

After some research and trials, I think it might work like this:

(1) Local variables and values in functions are destroyed when the function ends, unless they are referred to from outside the function. For example:

    // example 4
    var a;
    function myFunc() {
        var b = 1;
        var c = [1, 2, 3];
        a = c;
    }

    myFunc();   // variable b=1 is gone
    a;          // => [1,2,3], this value still exists

(2) The definition of a function somehow includes its "scope" which means the environment that it was defined in. So in example 1 and 2 of the original questions, the reason why "str" is remembered after the constructors are finished is NOT that the constructor functions remember them, but instead is because the "str" is remembered in the scope of each of the show functions.

Here is an example with no objects, just functions:

    // example 5
    function returnFunction() {
        var a = Math.random(0);
        return function () { return a; };
    }

    f1 = returnFunction();
    f2 = returnFunction();

    f1();  // => 0.79
    f2();  // => 0.21
    f1();  // => 0.79
    f2();  // => 0.21

Any clarification or links would be appreciated.

StormDog
  • 91
  • 6
0

Case 1 and 2 are all about closures. Variables are stored in so called call frames (a.k.a. scope chains).

If you know C/C++ then you can read my article "JS closures for C++ devs".

Otherwise check article of Richard Cornford.

c-smile
  • 26,734
  • 7
  • 59
  • 86