12 Replies Latest reply on Jun 27, 2006 3:19 PM by Newsgroup_User

    this keyword within Class

    ChrisFlynn Level 1
      Hi all

      Ive been learning (struggling) with AS 2.0 concepts lately, here's the latest.

      Say I have 2 arrays, half1 and half2, and I want the contents of these arrays end up in this other array, fullArr. How I want this to happen is I want to use the "pop" method on the "half" arrays to fill up the fullArr. But I want the "half" array to be randomly chosen.

      So if I were writing this as a framescript, it would look like this:

      for (var ii = (half1.length + half2.length); ii>=1; ii--) {
      whichOne = random(2)+1;
      if (this["half"+(whichOne)].length != 0) {
      //If there's still items left in this array, pop the last element back into fullArr
      fullArr[ii-1] = this["half"+(whichOne)].pop();
      } else {
      //If you're getting here, the "half" array that was randomly selected is empty, so pop the last element from the other "half" array
      if (whichOne == 1) {
      fullArr[ii-1] = half2.pop();
      } else if (whichOne == 2) {
      fullArr[ii-1] = half1.pop();
      }
      }
      }

      This works as a frame script. But this script breaks when its part of a Class. And I think it breaks at this line:

      if (this["half"+(whichOne)].length != 0) {

      It looks to me that using the 'this' keyword here is breaking everything. What's even stranger to me is that if instead of using 'this', I use the 'eval' keyword, and everything works as expected, in my Class.

      So this script works:


      for (var ii = (half1.length + half2.length); ii>=1; ii--) {
      whichOne = random(2)+1;
      if (eval("half"+(whichOne)).length != 0) {
      //If there's still items left in this array, pop the last element back into fullArr
      fullArr[ii-1] = eval("half"+(whichOne)).pop();
      } else {
      //If you're getting here, the "half" array that was randomly selected is empty, so pop the last element from the other "half" array
      if (whichOne == 1) {
      fullArr[ii-1] = half2.pop();
      } else if (whichOne == 2) {
      fullArr[ii-1] = half1.pop();
      }
      }
      }

      Now my confusion is about the 'eval' statements, I've always been under the assumption that 'eval' was going away and is bad form to use. I got Colin Moock's book and I don't think the term 'eval' is ever used in it (at least I couldnt find it). Is this bad form? Is there another way to do what I'm doing? Or am I right in doing this?

      I know this was long, but any help is always appreciated.

      Thanks
      Chris
        • 1. Re: this keyword within Class
          foozebox
          Hey..I think this is one of those things that is a bit simpler than it would seem to be. The first thing I would suggest is to use the 'concat' method of the Array to return a new array that combines the two (or three, four, etc. arrays).

          As for eval() vs. this[], I used to be a devout eval() guy and thought the same thing... but from what I've read, experienced, it doesn't make much difference memory-wise and eval() isn't deprecated so go nuts...whichever you prefer...

          Anyway, enough chatter here's some code:
          • 2. Re: this keyword within Class
            ChrisFlynn Level 1
            Thanks for the help. But I just wanted to clarify something.

            Its not that I'm looking for a preference bewteen this[] and eval(). My question is where I've used this[] so often in the past as a frame script, but why doesn't it work within a Class? and why does eval() work instead?

            Thanks again
            Chris
            • 3. Re: this keyword within Class
              foozebox Level 1
              Sorry looking back on that post I kind of misunderstood the problem... In a class, 'this' refers to the Class object itself, not the main timeline. Therefore if you fed the Class a scope (i.e. 'this') you can use that scope to return properties of itself. Therefore, 'this' isn't a magic word, it's just a reference to something that has properties that are accessed with []. So now this[prop] will be transposed as scope[prop]

              Have a look:

              (in a Flash Document:)

              var l:parseRandom = new parseRandom(this);

              (the Class:)

              • 4. Re: this keyword within Class
                Peter Lorent Level 2
                >>But this script breaks when its part of a Class.
                Please define breaks. Generate errors? Code doesn't produce the desired result?
                I've tested the code in a class and it works (at least without errors and with a result). Maybe you can post the full class or at least an excerpt.
                • 5. Re: this keyword within Class
                  ChrisFlynn Level 1
                  The break I'm referrring to is when the original code with the this[] statements, when it gets to here:

                  if (this["half"+(whichOne)].length != 0) {
                  //If there's still items left in this array, pop the last element back into fullArr
                  fullArr[ii-1] = this["half"+(whichOne)].pop();
                  } else {

                  My guess is that it doesn't know what to make of this, because when I do a trace for the values in fullArr afterwards, they all return 'undefined'. So my thought was within a Class, this["half"+(whichOne)] doesnt mean a lot.

                  Sorry I couldnt post more, I dont have it here in front of me right now. Maybe I'll post it later when I'm home.

                  Thanks
                  Chris
                  • 6. Re: this keyword within Class
                    Peter Lorent Level 2
                    I get a result when I trace fullArr. It's best to post the class you are working with.
                    • 7. Re: this keyword within Class
                      Peter Lorent Level 2
                      Maybe why the code breakes is that the arrays aren't defined as properties of the class.

                      class SomeClass{
                      private var half1:Array;
                      private var half2:Array;
                      private var fullArr:Array;

                      //constructor
                      public function SomeClass(){
                      half1=["content 1","content 2","content 3"];
                      half2=["content 4","content 5","content 6"];
                      fullArr=new Array();
                      }

                      public function traceArr(ind:Number):Void{
                      var whichOne:Number = random(2)+1;
                      trace(this["half"+whichOne][ind]);
                      }
                      }

                      With the this keyword you refer to members of the class. You can't use 'this' to refer to variables that are local to the function which I suspect is the case in your code.
                      • 8. Re: this keyword within Class
                        ChrisFlynn Level 1
                        OK so this is starting to make some sense. Because in my original code, the half1 and half2 arrays are declared as local variables to the function their in, not a private variable to the class the way you just posted. I declared them local to that function because they dont need to be accesses or referenced outside of that function.

                        So going back to my original thought, since eval("half"+whichOne)... seems to work, is that bad form in your opinion? Or is there another approach? Or should I have those half1 and half 2 arrays declared as private variables to the Class rather than local variables of the function?

                        Thanks for the help, I really do appreciate this.

                        Chris
                        • 9. Re: this keyword within Class
                          Peter Lorent Level 2
                          >>is that bad form in your opinion?
                          I don't think it is bad form. I can't recall if I read something about usage of eval() other than you can't use it at the left side of an equation. I must say I never use eval() and always [] and I would probably use the arrays as properties of the class. Depends on the usage in the app.
                          If you only need the arrays locally and just want to return a result... I might use eval() too.
                          Other ways: hard to tell. I would have to know more about the content of the arrays, the purpose, usage in the class.

                          • 10. Re: this keyword within Class
                            ChrisFlynn Level 1
                            Thanks again for the help.
                            • 11. Re: this keyword within Class
                              Peter Lorent Level 2
                              You're welcome.
                              • 12. Re: this keyword within Class
                                Level 7
                                > half1 and half2 arrays are declared as local variables

                                That is your whole problem then .. it has nothing to do with classes etc

                                you cannot use [ ] notation to get at local variables, because they are not
                                members of an object.

                                you HAVE to use eval()

                                it would have been very easy to verify because if
                                this.half1
                                fails then
                                this["half"+1"]
                                will fail, and vice versa. And this.half1 would not work as half1 is not a
                                member of the class
                                --
                                Jeckyl