A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: variables used in functions

Threaded View

  1. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The narrow scope of the function takes precedence over the bigger scope of the frame or class. The compiler won't get confused. But you might.

    To reference the instance-scoped variable within the function, use "this.type". If it isn't a property of the instance (such as a variable declared in another enclosing function), you can't reference it directly, you'd have to save it to another variable.

    Code:
    public class ScopeTest{
    
      private var foo:String;
    
      private function ScopeTest(){
         foo = "instance value";
      }
    
      private function testMethod(foo:String):void{
        trace("testMethod result: "+foo);
      }
    
      private function testNested(foo:String):void{
         trace("outer testNested value: "+foo);
         var bar:String = foo;
         trace("class value is still: "+this.foo);
         function nested(foo:String):void{
            trace("inner testNested foo value: "+foo);
            trace("inner testNested bar value: "+bar);
         }
         nested("nested inner value");
      }
    
      public function test():void{
         trace("class instance value: "+foo);
         testMethod("method param value");
         testNested("nested outer value");
      }
    }
    invoke as such:
    Code:
    var st:ScopeTest = new ScopeTest();
    st.test();
    should output:
    Code:
    class instance value: instance value
    testMethod result: method param value
    outer testNested value: nested outer value
    class value is still: instance value
    inner testNested foo value: nested inner value
    inner testNested bar value: nested outer value
    Last edited by 5TonsOfFlax; 03-17-2012 at 02:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center