3 Replies Latest reply on Nov 21, 2009 4:04 PM by shintashi

    create objects using functions?

    shintashi Level 1

      I tried turning the following object (clear sake) into the product of a function (see below),

      however, my attempts to display any kind of output produces "undefined".

       

      ------------ old code ----------------

      drink = new Object();
      drink.原酒 = new Object();
      drink.原酒.nm = "原酒";
      drink.原酒.dis = ???; //display price;
      drink.原酒.prc = 40; //actual price;
      drink.原酒.cal = 980; //whole bottle
      drink.原酒.alc = 156; //alcohol: % alcohol * calories;

      trace(drink.原酒.nm);

       

      //works

       

      ------------ new code ----------------

       

      drink = new Object();

       

      function booze(mc, a, b, c, d, e){
      mc = new Object();
      mc.nm = a; mc.dis = b; mc.prc = c; mc.cal = d; mc.alc = e;
      }

       

      booze(drink.原酒, "原酒", "???", 40, 980, 156);


      trace(drink.原酒.nm);

       

      //fails

        • 1. Re: create objects using functions?
          Ned Murphy Adobe Community Professional & MVP

          Try:

           

          drink = new Object();


          function booze(mc, a, b, c, d, e){
               mc[a] = new Object();
               mc[a].nm = a;
               mc[a].dis = b;
               mc[a].prc = c;
               mc[a].cal = d;
               mc[a].alc = e;
          }


          booze(drink, "原酒", "???", 40, 980, 156);


          trace(drink.原酒.nm);
          trace(drink.原酒.dis);

           

           

          I believe the problem in your version is that you are passing an undefined object into the function.  If you had created drink.原酒 = new Object(); before calling the function it would have worked because then you are passing a defined object into the function.

          • 2. Re: create objects using functions?
            shintashi Level 1

            Ned, that's amazing stuff!

             

            Is there a place to find out where to use stuff like the brackets "[ ]" and when it's appropriate to use things like quotes, Num/String, etc? I run into these syntax problems a lot (especially with for-loops and Arrays), and keep thinking I missed out on the Great Book of Syntax or something.

            • 3. Re: create objects using functions?
              Ned Murphy Adobe Community Professional & MVP

              It'd be nice if there is a book that has all the tricks of the trade nicely presented, but I don't own it if there is... I don't care for books.  Syntax is something that I've just come to understand thru years of losing hair trying to solve things.

               

              That bit with the brackets, which I call bracket notation, and others call array notation, comes in very handy when you want to use a string to target an object.  When you learn how to use them you end up finding that the syntax is just like what you would use for an array.  In your case, there was the string b variable, which you were using to name an object within the drink object (I assumed it was the same set of characters as those for the object you were naming).

               

              So when I was using mc[a], the program was translating it as mc.原酒, which in turn became drink.原酒

               

              The brackets cause the program to interpret whatever string is inside them as an object's name (or variable, or property... some named entity).  As far as the syntax goes, it is just like using an array index specification, wherein there is no "." before the brackets, but there is after if there is more to the object being targeted...

               

              for(i=0; i<10; i++){

                   someMC["childMC"+i]._visible = false;

              }

               

              In that example, I'm turning 10 child movieclips within someMC invisible.  Their instance names are childMC0, childMC1, childMC2,etc....  Notice that there is no decimal point ahead of the brackets, but there is after to connect the _visible property.  I don't know why it's like that, but it's memorized that way after years of using it.