2

I came across to this example:

function countMyself() {
// Check to see if the counter has been initialized
if ( typeof countMyself.counter == 'undefined' ) {
    // It has not... perform the initialization
    countMyself.counter = 0;
}

// Do something stupid to indicate the value
alert(++countMyself.counter);
}

The snippet above demonstrate "how to implement static local variable in javascript"

I know that function variables are stored in the stack. Having C background I know that variables in the stack can be easily overwritten by consequent function calls.

This doesn't seem to be the case with javascript.

What rule specifies how long a local(function) variable live in the lifetime of the program? I mean stack in Javascript must bear different semantic than stack in C, C++?

8
  • 8
    That's not really a local variable; it's a property of the function object itself. Functions are objects, and their properties last until something changes or removes them. Commented Aug 13, 2018 at 15:07
  • "Having C background I know that variables in the stack can be easily overwritten by consequent function calls." I'm not really sure what that means; it's not that they're "overwritten", which would imply a consistent memory location, it's that there's a new stack frame. In that sense JS is exactly the same, the difference being what Pointy said: this isn't a variable "local to the function" in the way you think it is. "Local function variables" in JS look and act roughly as they do in C. Commented Aug 13, 2018 at 15:10
  • @DaveNewton In C to return reference to a local variable(object) is idiotic. In JS this i common practis. Commented Aug 13, 2018 at 15:12
  • @Hairi setting a property of a function is not common practice in JavaScript. Commented Aug 13, 2018 at 15:17
  • @Pointy "something changes"? What something could be? Commented Aug 13, 2018 at 15:18

1 Answer 1

2

Local variables at least exist as long as they are accessible. Through closures they could even stay forever:

   function noClosure() {
     let local = 3;
     //...
   } // local gets recycled here

   function closure() {
     let local = 3;
     return function inner() {
       return local; // <- closured
    }
  }

  var closured = closure();
  // local exists here:
  console.log(closured()); // 3
  // but now it might get recycled:
  closured = undefined;

In your snippet that isn't actually a local variable but rather the property of a global function object, which exists until it gets deleted, becomes unreferencable, or the engine stops executing:

  delete countMyself.counter; // property deletion
  countMyself = somethingNew; // unreferencable
Sign up to request clarification or add additional context in comments.

1 Comment

(I added "unreferenceable" to cover when the function declaration is overwritten.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.