6 Replies Latest reply on Sep 5, 2006 11:10 AM by Newsgroup_User

    variable scope

    Goo101
      If I declare some variable outside an 'if' statement, is their scope still valid if I try to use them within that 'if' statement?
        • 1. Re: variable scope
          Level 7
          Goo101,

          > If I declare some variable outside an 'if' statement, is their scope still
          > valid if I try to use them within that 'if' statement?

          Your question isn't specific enough. If you're writing a function and
          scope your variable to the function, then any if() statement inside that
          function will "see" the variable. Not sure if that's what you mean, though.

          e.g.
          function myFunction():Void {
          var myVar:String = "example";
          if (myVar == "example") {
          // do something.
          }
          }


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: variable scope
            kglad Adobe Community Professional & MVP
            the if-statement does not affect the scope of anything.
            • 3. Re: variable scope
              Goo101 Level 1
              David,
              Yes, this is what I meant, I had declared variables inside the scope of a function, would they still be visible in an 'if' statement within the function. Evidently they would.
              I think the problem was the declaration of one function inside another. I therefore moved one function declaration outside of the function and then just called it from inside the other function and it worked ok.
              Thanks for your input though.
              • 4. Re: variable scope
                Level 7
                Goo101,

                > I think the problem was the declaration of one function
                > inside another.

                That makes sense, doesn't it? Variables scoped to a given function are
                only visible to that function.


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: variable scope
                  Goo101 Level 1
                  David,
                  Can one function be defined and executed from inside another function? Will it work? And will the variables have the appropriate scope to be visible to the nested function?
                  • 6. Re: variable scope
                    Level 7
                    Goo101,

                    > Can one function be defined and executed from inside
                    > another function? Will it work?

                    You betcha.

                    > And will the variables have the appropriate scope to be
                    > visible to the nested function?

                    It works from inside out. Paste the following into frame 1 of a FLA to
                    see an illustration.

                    outer();
                    function outer() {
                    var varOuter:String = "outer";
                    trace("traced from outer()");
                    trace("varInner: " + varInner);
                    trace("varMiddle: " + varMiddle);
                    trace("varOuter: " + varOuter + "\n");
                    middle();
                    function middle() {
                    var varMiddle:String = "middle";
                    trace("traced from middle()");
                    trace("varInner: " + varInner);
                    trace("varMiddle: " + varMiddle);
                    trace("varOuter: " + varOuter + "\n");
                    inner();
                    function inner() {
                    var varInner:String = "inner";
                    trace("traced from inner()");
                    trace("varInner: " + varInner);
                    trace("varMiddle: " + varMiddle);
                    trace("varOuter: " + varOuter);
                    }
                    }
                    }


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."