A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: variables used in functions

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    85

    variables used in functions

    I have a stupid question. I got this variable:

    Code:
        var type:int;
    and this function:
    Code:
        private function test(type:int):void {
        }
    when i call the function and throw in my variable type
    Code:
        test(type));
    is there a possibility that the compiler gets confused because my variable has the same name like the functions's parameter (type = type)? i'm not certain about this topic. Normally I'd write my function's parameter like this (if such a case occurs):
    Code:
        private function test(_type:int):void
    just to make sure the names do not exactly match (well I hope you know what I mean).

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Well I'm assuming you mean something like this
    PHP Code:
    var awd:int;

    test (awd);

    function 
    test (awd:int) {
        
    awd 1;

    I don't know what would happen, you could just test it easily. I just add an underscore or capitalize the first letter of the argument name if this happens.

  3. #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.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    85
    Thanks for your answers! Finally I got it now.

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