A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: What the var?

  1. #1
    Senior Member
    Join Date
    Oct 2002
    Location
    New York
    Posts
    117

    What the var?

    Do you ever have to use the var command?

    What's the difference between:

    favoriteSite = flashkit;

    vs.

    var favoriteSite = flashkit;

    I didn't think you ever had to use the var command. Does it have something to do with loading external variables?

    Thanks

  2. #2
    FK's Official War Driver wiReZ's Avatar
    Join Date
    Jun 2002
    Location
    wireless
    Posts
    615
    You know, that is a good question! I hardly ever use var, I just declare the vars as is. I wonder what the relevance is behind using var
    My current rig: AMD Athlon64 3500+(Winchester Core) MSI K8N Neo2 Platinum - 1GB
    (2x512MB) Kingston HyperX DDR333 BH5 - ATI All-In-Wonder 9800 Pro 128mb - SB Audigy 2
    WD 80GB 7200RPM 8MB - WD 250GB 7200RPM 8MB - MAX 120GB 7200RPM 8MB - Dell 2001FP 20.1 LCD

  3. #3
    The world goes - hm.. TheCaramella's Avatar
    Join Date
    Dec 2001
    Location
    Second door on the right, next to the ice bear
    Posts
    642
    It's got nothing to do with external or internal variables.
    The word var is used to declare a variable, and to declare it as local when needed.
    That is because Java script is a very loose typed language, and it's the only way(?) to avoid variables being confused with (more) global variables (can't think of any others right now, except for variables being created and lasting just inside some function, during the lifetime of that function)
    This link gives a pretty good answer:https://lists.latech.edu/pipermail/j...il/003376.html

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    var is a local variable, which is only executed within a function. Here is an example:

    var i=1;

    function test(){
    if(i<10){
    trace("i is smaller than 10");
    }

    this does not work because i will not be recognized in the function but this works:


    i=1;

    function test(){
    if(i<10){
    trace("i is smaller than 10");
    }

    So use var only within functions and not if they are supposed to be global.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    The world goes - hm.. TheCaramella's Avatar
    Join Date
    Dec 2001
    Location
    Second door on the right, next to the ice bear
    Posts
    642
    Almost, cancerinform
    var can be used for declaring a java-/action script variable local. But it can also be used outside a function or another block of code (f. i. loops, if/else etc.)
    If it's used inside a block of code, this variable will excist there, only. It will not change the value of any variable outside the block of code, with the same name. That's why one would use the keyword var inside a block of code. It will therefore overrule the value in a more global variable with the same name, because the program got the strict command: THIS variable is THE variable - for now, in this block of code, in this scope.
    (This is easier in more strictly typed languages as C (++, #) or Java. I've spent some time on figuring out what this means, 'cause I was so confused and frustrated for a while. That's why I think I know now )

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    That,s what I meant basically a block of code. But when I mean blocks of code I usually mean functions. I forgot another example. This also works:

    function test(){
    var i=1;
    if(i<10){
    trace("i is smaller than 10");
    }
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    The world goes - hm.. TheCaramella's Avatar
    Join Date
    Dec 2001
    Location
    Second door on the right, next to the ice bear
    Posts
    642
    Ok, I think I misunderstood you

  8. #8
    Flash Product Manager
    Join Date
    Mar 2002
    Posts
    140
    To expand:

    Not using the var keyword makes your variable <i>relative</i> to the current timeline (where you're writing your script).

    Ex: (writing this script on frame 1 of the main timeline)

    message = "Hello World!";
    // "message" now exists relative to _root so it can be referenced
    // from anywhere as _root.message

    If you use the "var" keyword it makes the variable local in scope, which means it expires after the object it is local to expires. So, if you use the var keyword outside of a code block, that variable becomes local to that timeline. If that timeline happens to be _root, your local variable will exist throughout the life of the main timeline. In such case, the effect is the same as not using the var keyword.

    Ex: (writing this script on frame 1 of the main timeline)

    var message = "Hello World!";
    // "message" still exists relative to _root

    If you use the "var" keyword inside of a code block the variable becomes local to that object. So if you're writing code inside of the Function object, your variable will only exist while the function's code is being executed, then it will expire

    Ex: (writing this script on frame 1 of the main timeline)

    function doSomething(){
    var message = "Hello from inside the function";
    }
    // the variable message is not accessible from anywhere else
    // in the movie - it only exists within the function when it
    // is called

    There is also a new GLOBAL scope referenced as _global.

    Ex: (writing this script on frame 1 of the main timeline)
    _global.message = "Hello Universe!";
    // this variable can now be accessed from anywhere in the movie

    When you declare a variable in the _global scope it can be accessed from anywhere at any time - including from movies loaded into other movies using the loadMovie() method. This is incredibly useful as it allows developers to permanently store and share information throughout an application.

    When you refer to a variable in AS, the interpreter first checks the local scope, then the relative timeline, then the _global scope. When you want to refer to a _global variable, you don't have to prefix the reference with "_global" - once it has been declared. However, coding an absolute reference to _global speeds up the parser as it doesn't have to waste time checking the for local variables first.

    Hope that helps...

  9. #9
    FK's Official War Driver wiReZ's Avatar
    Join Date
    Jun 2002
    Location
    wireless
    Posts
    615
    Originally posted by MikeDowney
    ...
    Nice explanation
    My current rig: AMD Athlon64 3500+(Winchester Core) MSI K8N Neo2 Platinum - 1GB
    (2x512MB) Kingston HyperX DDR333 BH5 - ATI All-In-Wonder 9800 Pro 128mb - SB Audigy 2
    WD 80GB 7200RPM 8MB - WD 250GB 7200RPM 8MB - MAX 120GB 7200RPM 8MB - Dell 2001FP 20.1 LCD

  10. #10
    The world goes - hm.. TheCaramella's Avatar
    Join Date
    Dec 2001
    Location
    Second door on the right, next to the ice bear
    Posts
    642
    Originally posted by MikeDowney
    (...) coding an absolute reference to _global speeds up the parser as it doesn't have to waste time checking the for local variables first.
    Does this mean that all variables and functions I usually declare at the main timeline, to be "seen" everywhere in the movie, would do better if I declared them _global?

  11. #11
    Flash Product Manager
    Join Date
    Mar 2002
    Posts
    140
    Originally posted by TheCaramella
    Does this mean that all variables and functions I usually declare at the main timeline, to be "seen" everywhere in the movie, would do better if I declared them _global?
    Not necessarily. I think the point that I was trying to make is that <i>if</i> you use _global variables, make sure you reference them by prefixing the reference with an absolute pointer to _global. All that does is tell the parser to go directly to the global scope when looking for the variable, instead of checking for its existance in local, timeline then global.

    As a general rule of thumb, you should always try to make your variables local - to save memory. However, if you'd truly like to make variables and functions globally available from any timeline or any loaded movie, then I'd suggest using the global scope instead of references to timelines. This, more than anything, makes for much cleaner code and a much more non-linear approach to programming.

    Hope that helps...

    Cheers,
    MD

  12. #12
    The world goes - hm.. TheCaramella's Avatar
    Join Date
    Dec 2001
    Location
    Second door on the right, next to the ice bear
    Posts
    642
    Never stops, you know
    Now:
    if you use _global variables, make sure you reference them by prefixing the reference with an absolute pointer to _global
    Do you mean that I should reference a global variable with the keyword _global, like this:
    Code:
    (...)
    if(_global.myVariable < someThing){
      bla-bla
    }
    ?

  13. #13
    Flash Product Manager
    Join Date
    Mar 2002
    Posts
    140
    Originally posted by TheCaramella

    Now: Do you mean that I should reference a global variable with the keyword _global, like this:
    Code:
    (...)
    if(_global.myVariable < someThing){
      bla-bla
    }
    ?
    Yes, <i>if</i> you want to speed up the parser. Remember that it is not <i>necessary</i>, but it will speed things up.

    Cheers,
    MD

  14. #14
    Junior Member
    Join Date
    Apr 2003
    Posts
    2
    So, if you mistype a variable name you declare a brand new global variable.

    Doesn't anyone else think this is amazingly stupid? I'd love to know if there was something akin to Perl's (ugh) "use strict" to prevent this kind of madness.

  15. #15
    Flash Product Manager
    Join Date
    Mar 2002
    Posts
    140
    Originally posted by fzr
    So, if you mistype a variable name you declare a brand new global variable.
    No, that's not the case. Again, to review, declaring a variable using the "var" keyword makes it local to the current timeline or object it is declared within. Not using var defaults the variable's scope to the current timeline only. This means that creating a variable inside of a function block w/out using the var keyword makes that variable available outside of the function.

    However, you can place a variable within the _global object (scope) by explicitly referencing _global when creating the variable. This means that said variable can be accessed from anywhere in the running movie simply by referencing _global.<i>variable name</i>.

    It's important to understand, however, that the Flash run-time engine has to search for every variable that you reference from within your code. It starts it's search by checking the local scope. If it does not find the variable in the local scope it next checks the relative timeline. If it does not find the variable there, it checks the _global scope. This whole process takes time and processor power. A good way to eliminate this search is to explicitly point at exactly where your variable lives every time your reference it in your code. Since _global is the last place the parser checks, explicitly telling the parser that your variable is "_global.foo" instead of just "foo" will eliminate the search and speed up your app.

    Does that make more sense?

    As a side note, you should be cautioned not to use the _global scope AT ALL if developing Flash applications for <a href="http://www.macromedia.com/software/central">Macromedia Central</a>. This is due to the way the Central run-time works (and would take far too long to explain here).

    Cheers,
    MD

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Mike Downey
    Producer | Evangelist
    Macromedia On Demand
    http://www.macromedia.com/ondemand
    mdowney@macromedia.com

  16. #16
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Mike,

    thanks for your nice explanation. I am developing components and in one of my components I have used global variables so I can change the var using actionscript from outside. That worked fine so far. But then I changed it according to your scheme targeting the variable from outside using the instance name and that does not work. Within the component and its assets it worked fine. Do you know a solution?
    - The right of the People to create Flash movies shall not be infringed. -

  17. #17
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I solved the problem I am now eliminating _globals in my scripts and components. By the way would it be possible that macromedia sometimes posts important messages like the one about _globals in some forums like this !
    Thanks

    Joachim
    - The right of the People to create Flash movies shall not be infringed. -

  18. #18
    Flash Product Manager
    Join Date
    Mar 2002
    Posts
    140
    By the way would it be possible that macromedia sometimes posts important messages like the one about _globals in some forums like this !
    That's a good idea, but I think informational postings like that are more likely to be found on the <a href="http://www.macromedia.com/support/flash/ts/documents/tn4149-flashnews.html">Macromedia Forums</a>.

    Good luck!

    MD

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