A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: The one acceptable use for the name property

Threaded View

  1. #1
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801

    The one acceptable use for the name property

    There is one and only one decent use for the name property. That is to get a reference to something which was manually placed on stage. If there's another good use, I'd like to hear it.

    Here are some bad uses:
    1. In place of variables or properties.
    Don't throw away references if you have them and will need them later.
    Code:
    something.x += 50;
    is much better than
    Code:
    getChildByName("something").x += 50;
    or
    Code:
    this["something"].x += 50;
    The latter two can throw errors at runtime that would be caught at compile time using a real reference.

    1b. In place of references
    You should really think hard about your design if you have something like this:
    Code:
    var people:Array = ["alice", "bob", "carol"];
    when you could have this instead:
    Code:
    var people:Array = [alice, bob, carol];
    Hint: do you really want the name, or do you want the person?



    2. As a collection substitute. Here I include sequentially named variables as well as instancenames.
    If you have things named with sequential numbers, you almost certainly want to deal with them as a collection. Put them in an Array or Vector.
    If you have this:
    Code:
    var thing1:Thing = new Thing();
    var thing2:Thing = new Thing();
    You should do this instead:
    Code:
    var things:Vector.<Thing> = new Vector.<Thing>();
    things.push(new Thing());
    things.push(new Thing());
    You can use an Array instead of Vector if you are targeting < flash 10.

    3. As a substitute for any other data structure.
    If you have variables like bobThumb and bobLoader, do not rely on the fact that they are bobSomethings to tie them together. You should create a class which has properties for all the things you need to associate, but if you have oophobia, you can use a Dictionary to associate one object with another.
    Code:
    var bob:CompositeThing = new CompositeThing();
    bob.thumb = something;
    bob.loader = new Loader();
    Code:
    public class CompositeThing extends Sprite {
      public var thumb:Sprite;
      public var loader:Loader;
    
      //class constructor and more methods here.
    }
    Via dictionary
    Code:
    var loadersByThumb:Dictionary = new Dictionary();
    loadersByThumb[bobThumb] = bobLoader;
    The next person who asks how to generate sequentially named variables dynamically will be smacked with a ruler.
    Last edited by 5TonsOfFlax; 02-23-2011 at 04:02 PM.

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