Web Analytics Made Easy -
StatCounter is there trick to access function local var ? - CodingForum

Announcement

Collapse
No announcement yet.

is there trick to access function local var ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • is there trick to access function local var ?

    Hello
    just wandering ..
    is there any way to access function local var without passing it as arg ? some wird trick?
    like if i have :
    -------------------------------------
    function foo1()
    {
    alert(....here i want to alert the foo2 z var ....);
    }

    function foo2(x)
    {
    var z=x;
    }


    foo2(5) ;
    foo1();
    -----------------------------------------------
    thanks

  • #2
    this might do what you want, but i can't imagine a use for it:

    foo.bar = "some value";
    function foo(x) {
    this.bar = x;
    }

    function bar() {
    alert(foo.bar);
    }

    i didn't test that at all, and the logic i'm basing it on is a bit sketchy, so you may have to play with it a bit, and it may just plain not work.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

    Comment


    • #3
      You can't access a local variable in a function while that function isn't running - for the simple reason that it doesn't exist. Local (function) variables are properties of that function's Call object, which is created when the function is, what else, called - and destroyed when it returns. The only way to get the value to persist is to stick it somewhere, e.g., as a property of the Function object itself:

      function foo1() {
      alert(foo2.z);
      }

      function foo2(x) {
      var z = x;
      foo2.z = z;
      }

      foo2(5);
      foo1();

      Comment


      • #4
        thanks for seconding my opinion, adios. i thought that would work, but i wasn't sure.
        bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

        i am a loser geek, crazy with an evil streak,
        yes i do believe there is a violent thing inside of me.

        Comment


        • #5
          Wasn't seconding anything, just trying to explain the principle involved, without using the word 'I' repeatedly...

          Comment


          • #6
            seems to me that if you define a variable outside of any function then it has global scope - well "file" scope is a more correct term IMHO. Thus one can access the variable w/out passing it as a parameter.

            Comment

            Working...
            X