A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] Dynamically create functions

  1. #1
    Member
    Join Date
    May 2006
    Posts
    70

    resolved [RESOLVED] Dynamically create functions

    I'm trying to create functions dynamically, but I'm running into some problems. Basically I have a loop, and I'm trying to create functions within that loop. Here is the code:
    Code:
    for (i=0; i < 10; i++){
    var newFunction = "result" + i;	
    
    function newFunction(event:MouseEvent ){
    //do something
    }
    }
    So the end result should be a set of functions called "result0", "result1", "result2", and so on...).

    I've tried different variations of that code, but with no luck. If anyone has any ideas, it would be appreciated!!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No. Bad. Bad programmer. No treat.

    I know what you think you're tyring to do, but you shouldn't do it. Even if it was a good idea (it's a terrible idea), the way you're approaching it is wrong on many levels.
    You declare a variable newFunction, and then assign it a String value. You then declare a function also called newFunction and do absolutely nothing with it. How did you expect to connect your function to the name?

    If you do want to create functions dynamically, you can do that. But any time you think you want to name things sequentially, you're wrong. What you really want is a collection like an array.

    You should always give your variables types. If you had, you'd have seen that you were creating a String value when you wanted a Function.
    Code:
    var results:Array = [];
    for (var i:int =0; i<10; i++){
      results.push (makeFunction());
    }
    
    function makeFunction():Function {
      return function (e: MouseEvent):void{
        //do stuff
      }
    }
    Note that three is no reason to have multiple functions here since all will be identical. You could make unique functions, though. But you'll probably be better off with one that uses a parameter to get its info.

  3. #3
    Member
    Join Date
    May 2006
    Posts
    70
    I left out a lot of the code to simplify things, and each function will not be identical.

    I appreciate your reply, but it didn't answer my question. I just need to figure out how to create functions dynamically.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, it did. makeFunction returns a new, unique, function. You can do anything you like with that, including putting it in an array, like I show.

  5. #5
    Member
    Join Date
    May 2006
    Posts
    70
    Maybe I'm missing something, but I don't see how your code will provide what I am trying to do. If you could help me re-write the code I provided to create functions called "result0", "result1", etc, that would be great. (And without using an array).

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    No, I won't do that, because you absolutely should not do that.

    Oh, fine. See if this makes sense.
    Code:
    var result1:Function = makeFunction();
    Sequentially named stuff is not scalable, harder to use, and downright stupid.

  7. #7
    Member
    Join Date
    May 2006
    Posts
    70
    Do you always act like this much of a prick when you are "helping" someone? No one asked you if what I'm trying to do is "stupid" or a "terrible idea". I simply asked how to adjust my code.

    Then again I see you will "moderate for beer", so maybe you are just drunk.

    Please don't respond to this thread anymore. I would rather get help from someone else. Now if anyone without a self righteous attitude like our friend here would like to help me adjust the code I originally provided, I would really appreciate it.

  8. #8
    Member
    Join Date
    May 2006
    Posts
    70
    Nevermind, got it figured out.

  9. #9
    Member
    Join Date
    Sep 2010
    Posts
    32
    @ huflungpu35
    plz resign or go somewhere else,
    5TonsOfFlax been here and helping ppl for free so show some respect,
    if he thinks your an idiot guess what you are,
    respect is earn and since you only have 68 post which are most likely asking "stupid" question!
    i mean if you want to find out how to create dynamic function
    Dude there is a thing called google fyi, unless you been hiding under a rock

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Hold on there. Please remain civil. There's a big difference between saying he's trying to do something stupid vs calling someone stupid.

    If he doesn't want me to answer his questions, that's fine. I probably did get a little aggressive there, but I was just trying to help him avoid going down a bad path.

    Anyway, this issue has been marked resolved, so I don't think it needs further discussion. If you'd like to discuss personal conduct, please keep it to private messages.

  11. #11
    Member
    Join Date
    May 2006
    Posts
    70
    Quote Originally Posted by onenonly View Post
    @ huflungpu35
    plz resign or go somewhere else,
    5TonsOfFlax been here and helping ppl for free so show some respect,
    if he thinks your an idiot guess what you are,
    respect is earn and since you only have 68 post which are most likely asking "stupid" question!
    i mean if you want to find out how to create dynamic function
    Dude there is a thing called google fyi, unless you been hiding under a rock
    No offense, but no one asked for your opinion. And I have to point out the irony of you calling me an "idiot", when you can barely write a complete sentence.

  12. #12
    Member
    Join Date
    May 2006
    Posts
    70
    Quote Originally Posted by 5TonsOfFlax View Post
    Hold on there. Please remain civil. There's a big difference between saying he's trying to do something stupid vs calling someone stupid.

    If he doesn't want me to answer his questions, that's fine. I probably did get a little aggressive there, but I was just trying to help him avoid going down a bad path.

    Anyway, this issue has been marked resolved, so I don't think it needs further discussion. If you'd like to discuss personal conduct, please keep it to private messages.
    And Flax, some of your comments were unnecessary, but I do appreciate you trying to help. I was a little aggressive in my last post as well, so I apologize. I was trying to finish up a project, and this was the easiest way to do what I needed.

    Anyway, I agree that there is no need for further discussion here.

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