A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [F8] Functions stored as variable

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Location
    Planet Earth
    Posts
    298

    [F8] Functions stored as variable

    This is probably a dumb question but....

    Just wondering what the benefit of storing a function as a variable, then assigning that variable to execute function vs. writing a function and calling as needed.

    these both can accomplish the same thing
    Code:
    // the function
    function makeItHappen(){
              me = "dummy";
    }
    
    // called from a button
    
    btn.onPress = function(){
            makeItHappen();
    }
    versus
    Code:
    // function assigned to variable
    makeItHappen = function() {
          me = "dummy";
    }
    
    btn.onPress = makeItHappen;
    Does one over any advantage over the other? Has the 12 straight hours of writing code finally sent me bonkers? If C-A-T really spelled dog, what would P-I-G spell?
    ---
    Thinking outside of the box will get you fired if the "box" is strict budget.

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    I recommend you read this post by Darron Schall.

    The main difference is that when you write it like this:
    code:

    btn.onPress = makeItHappen;


    you can't pass arguments to the function, so, the function shouldn't have any parameters.

    When you write it like this:
    code:

    btn.onPress = function(){
    makeItHappen(param1, param2, ...);
    }


    you can pass arguments to the function.

  3. #3
    Member
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    56
    ive always used it this way

    btn.onPress = function(){
    makeItHappen(param1, param2, ...);
    }

    i just find that neater.

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