9 Replies Latest reply on Aug 23, 2006 3:47 PM by sbryner

    Learning Prototype

    sbryner Level 1

      I'm learning about prototypes and have done much reading. Most I don't understand or read over and over to retain slooooowly.

      The below code is what I've come up with, with the help of Blemmo's direction.

      The problem I'm having is I want to add the all the properties of the "searched item" (ie.. bob's name) in myArray into a new array using the prototype.id method. So, if there is 2 bob's both are added into the res array with all their respective properties.

      When the code gets to return res; It doesn't return a value (that I can see).
      Help please, I'm sure I just don't fully understand .prototypes, yet.


        • 1. Re: Learning Prototype
          abeall Level 3
          You're calling myNewName.id() without passing any arguments, but the method, which I can't quite tell what you are trying to do with it or why, takes two arguments.
          • 2. Re: Learning Prototype
            Level 7

            "abeall" <webforumsuser@macromedia.com> wrote ...
            > You're calling myNewName.id() without passing any
            > arguments, but the method, which I can't quite tell what
            > you are trying to do with it or why, takes two arguments.

            Agreed. There's really not a lot to prototyping. If you're familiar
            with writing functions, you're 99% of the way there. When used as you've
            shown ...

            Array.prototype.id = function(prop, value) { ... }

            ... it means you're adding a function -- happens to be named id() -- to
            *all* instances of the Array class after this point. So any new array will
            now have an Array.id() method, in addition to built-in methods like
            Array.push(), slice(), sort(), and so on.

            That's all there is to it. If your id() method requires parameters and
            you fail to provide them -- this occurs here ...

            myNewName.id( where are the parameters? );

            ... -- then Array.id() can't do what it's expected to do.

            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."

            • 3. Re: Learning Prototype
              blemmo Level 1

              first, the function will only lead to results when the array contains objects. The lines
              myNewName =["myname", "bob"];
              won't return any results, because the array contains strings (also because there are no parameters, as abeall already wrote). You should use 'myArray' from the 1st line.
              Then there's an error in the 'if' part inside the 'while' loop. The 'return' statement ends the execution immediately, so the loop breaks as soon as the first matching item is found. If you want all items that have the specified property and value, the loop has to test every array item, and the result should be returned after the loop finished.
              Because 'return' ends the function, every code after it won't be executed, so the 'trace' command will never be reached. It should be placed before 'return'.
              Finally, as the function returns an array, you must assign it's result to a variable to use it. If no item matched, that variable will be an array with the length 0.

              • 4. Re: Learning Prototype
                sbryner Level 1
                so, if I take out:

                return res,

                it should check all "myname" properties for "bob"? correct?
                and when I use the code below it outputs


                why? the bob,myname?
                and doesn't

                res.push(this );

                add all the properties of the property "myname" with the value of "bob" to a new array named "res" so I could call them later? to say populate movieClips?
                I hope I'm not ticking off people with these redundant questions (smile).
                • 5. Learning Prototype
                  blemmo Level 1
                  res.push(this[ i ]);
                  will add the item at position i to the result array. That's just 1 item, not all. To get all items, the loop must test every item in the array, and add every match to the result array. That's why the 'return' statement should not be inside the 'if' part, because it will end the loop before it could test all items. It should come after the loop block.

                  Your code should just output "bob", because you don't pass any parameters to the id() call, so it tests for
                  myArray[ i ].undefined == undefined
                  which should always be true, and because 'return' is inside the 'if' part, it returns the first item it checked and finishs.

                  The lines
                  myArray = ["myname","bob"];
                  don't make sense, 'myArray' is already defined in the first line, and the id() call is missing the parameters. Use something like
                  var newArray = myArray.id("newname","bob")

                  • 6. Re: Learning Prototype
                    sbryner Level 1
                    ok, so below is my new code with the updated changes.
                    my output for it reads

                    4 // number of objects in original array to search through
                    [object Object] // is = to myname value
                    I'm in else // is != myname
                    I'm in else // is != myname
                    [object Object],[object Object] // is = to myname value

                    why is the second entry to my res array have 2 [object Object] and the first has only 1 [object Object]?
                    how can I see my properties for each in the output using trace (trace) like:

                    trace(res[0]) // outputs "bob","London"
                    trace(res[1]) // outputs "bob", "Newark"

                    • 7. Re: Learning Prototype
                      blemmo Level 1
                      The id() function goes trough the calling array and collects all matching items in the result array 'res'. So when 2 entries have the property "myname" with the value "bob", the result array contains those 2 entries. The loops goes through the array from the last position, so the first item it checks is the object {myname:"bob", town:"Newark", id:4}. This matches the given parameters, so it adds this object to the result array and shows one object in the output ([object Object]). The next items don't match and the message in 'else' is traced. The first entry in the array and last in the loop matches again, so the result array now contains 2 objects, which get traced as [object Object],[object Object].

                      A variable of type 'Object' always gets traced this way. To trace the properties of the objects, you must loop through the object properties or access them directly. The 'for ... in ...' statement is handy for this, it goes through all items in an array or object:
                      for (var prop in someObj){
                      trace ("property "+prop+" = "+someObj[prop]);
                      This will output the name of the property (prop) and its value (someObj[prop]). With one of your objects it would give
                      property myname = bob
                      property town = London
                      property id = 1

                      So instead of
                      inside the 'if' part, you could use
                      for (var entry in res){
                      trace ("-- array item "+entry+" --");
                      for (var prop in res[entry]){ // res[entry] is one of the objects, equal to 'someObj' in the code before
                      trace (prop+" = "+res[entry][prop]);

                      The 'for ... in ...' statement works on arrays and objects, so it can be used to loop through every array entry and then to go through all properties of each entry. That's what the above code does. Try it out and compare the output to the code lines, this should help to understand what's going on here.

                      In the code you posted, the return statement is misplaced, it has to be inside the code of the id() function, after the while loop. The returned array will be assigned to the variable you specify when you call the function, like
                      var result:Array = myArray.id("myname","bob");
                      The array 'result' will get the value of the array 'res' inside the id() function through the 'return res' line.
                      When you test your movie, you can also open the "Debug" menu in the swf window, click "Show variables" (or press Ctrl+Alt+V), and search for the variable 'result'. It should list the contents of that array (as references to the items in myArray).

                      • 8. Re: Learning Prototype
                        sbryner Level 1
                        Below is the new code:


                        1) why does it only return the first (actually last) "bob" on the list and no others. I understand it reads from last
                        entry to first entry. So, why is it not returning 2 "bob" s in the array with "bob" "Newark" and "bob" "London"?
                        did I forget something here?

                        2) I like the debug function and would probably never have found it without your help. Thanks, Blemmo.

                        It returns(with the code below): does it mean myArray object[0] = id:4 , town:"Newark", myName:"bob"? correct?

                        Level #0:
                        Variable _level0.$version = "WIN 8,0,22,0"
                        Variable _level0.myArray = [object #1, class 'Array'] [
                        0:[object #2, class 'Object'] {
                        Variable _level0.theID = 4
                        Variable _level0.theName = "bob"
                        Variable _level0.theTown = "Newark"

                        3) now, when I:

                        it returns the number 3 times, why not just once?

                        thanks, I'm slowly getting all this. I can read it and understand almost all of it but a lot seems to do with the way
                        you order what you want done and I think that is the hardest part.
                        • 9. Re: Learning Prototype
                          sbryner Level 1
                          Here is a link that might help show what I'm trying to do.

                          I've got it to pull up on item in the array and place it into text and loader components.
                          Now, I want the second, third..... in the list to load into another text and loader component(s).

                          check this out and tell me if I'm far from completing this little search and display results .swf.
                          SKY'S TEST FLASH .SWF PROJECT