3 Replies Latest reply on May 14, 2006 1:28 PM by dan_vaughan

    Creating Dynamically named functions

    dan_vaughan
      Hi,

      Somehow, I think this might be a difficult question to answer, let alone, a difficult question to ask...

      I want to know how to create a dynamic set of functions based upon a vaying set of identifiers. ie: I have a stage in which i dynamically load various images. I am loading the images into a html formated dynamic text field using the <img> tag in conjunction with the "id" identifier variable.

      Each of the images has an identifier assigned to it... say, for instance, addcart1, addcart2, addacart3... etc...

      Therefore I want to create a dynamically named function for each of the identifiers: ie:
      addcart1.onRelease = function(){...
      addcart2.onRelease = function(){...
      addcart3.onRelease = function(){...

      On one occastion, there may be 1 identifier, on other occasions, there may be 50. There may be zero.

      Secondary to this, I want to create variables within the functions, based upon the identifier name. ie the function for "addcart1" will have variables based upon the number 1, "addcart2" will have variables based upon the number 2... etc.

      Is this as clear as mud? If so, i'll re-post and try and clear this up...

      Egards,
      Dan
        • 1. Re: Creating Dynamically named functions
          dan_vaughan Level 1
          Well, after 2 hours of research, i'm almost there... I'm using this function...

          for (var s = 0; s <= table.length; s++) {
          eval("buy_txt.addcart"+s).onRelease=function () {
          trace("cart number = "+ s);
          }
          }

          This code is working great for defining the functions addcart1,2,3 etc...But because the function is not inline, the "s" counter gets to the end of the table length and stays there. So every function returns the trace "cart number = 22" (22 is the table.length.

          Well, it's answered most of my questions anyway...

          Daniel
          • 2. Re: Creating Dynamically named functions
            blemmo Level 1
            2 ways of solving this:
            a) store the matching 's' with the images:
            for (var s = 0; s <= table.length; s++) {
            eval("buy_txt.addcart"+s).id = s;
            eval("buy_txt.addcart"+s).onRelease=function () {
            trace("cart number = "+ this.id);
            }
            }
            b) use another function to assign the onRelease:
            function setRelease(object, id){
            object.onRelease = function(){
            trace("cart number = " + id);
            }
            }
            for (var s = 0; s <= table.length; s++) {
            setRelease(eval("buy_txt.addcart"+s), s)
            }

            Instead of eval(), you may also use array syntax like in the attached code.

            hth,
            blemmo

            • 3. Re: Creating Dynamically named functions
              dan_vaughan Level 1
              Blemmo,

              Thanks very much for the ideas, application and code. It does exactly what I needed. I really appreciate your input.

              Regards,
              Dan