flash: why use "var"?
- Started
- Last post
- 13 Responses
- smartK
the book I just bought tells me to write:
var loop = 1;why not just write:
loop = 1;
??
- ave0
It's always a good isea to keep your code clean and organised (wherever possible). Specifying var, lets you know where you have initiated a variable, rather than simply assigning a value to it.
If your script becomes hefty you could easily loose track of assigning vs. initiating....best thing I can think of.
- ave0
isea = idea, not ikea :)
- kpl0
var is more useful in blocks. if you have a bunch of variables in a block (ie, a function) that are not needed outside of it, declaring variables with var within that block means those variables will clear the memory once the block is finished.
ie, in
if (1) {
var blah ="blah blah";
}trace (blah);
result: blah is undefined
it's useful when you don't want certain variables hogging memory all the time.
admittedly, in real small scripts it doesn't make a whole lot of difference.
- estlin0
as far as my knowledge of it...
var is useful in that the memory used by the variable is released after the execution of the actionscript call it's contained in. might or might not significantly speed things up, haven't done real world tests, but in theory it should be more efficient.
you only use it if you don't need to reference the variable outside of its local scope.
- ave0
...the scope (life) of a variable is determined by the position of that variable within the movie. A variable contained within a function lasts for the duration of that function (local scope), a variable located outside of any functions takes on the scope of its parent timeline.
Using "var" has no bearing on the life of a variable. It's coding edicate with actionscript - and let's not forget to comment our code!
- kpl0
hmm, interesting. I know the way I described it is how it works in javascript, which actionscipt is based off of...i think
- thanton0
ave's wrong, it does have to do with scope
http://www.macromedia.com/suppor…
- ave0
hmmmm...
- enobrev0
i think the "var" in actionscript is implied. Just like the semi-colon.
As in, use it if you're used to using it, but you don't necessarily have to.
Because the explanation given tat the macro site is regarding vars in general and not necessarily the usage of the var command.
- subaddiction0
It is used for local variables. put this in your timeline:
test =function(){
var testme="good"
trace (testme);
}
test();
trace (testme);and youll get:
good
undefineduse this
function test =function(){
this.testme="good"
trace (testme);
}
test();
trace (testme)
then you'll get
good
goodAnd it would works just the name using:
function test(){
var testme="good"
trace (testme);
}
test();
trace (testme);or
function test(){
testme="good"
trace (testme);
}
test();
trace (testme);
- subaddiction0
sorry that line should read:
"it would WORK just the SAME.."
- subaddiction0
oh.. and the second function declaration should read
test=function()
instead of
function test =function()
sorry, i just wrote the code in this little window... i should check my work i suppose?
- tjk_work0
In addition, var will save you if you for some reason use a predefined piece of actionscript syntax as a variable name.
i.e.
loadMovie = something
this will cause an error because actionscript thinks your calling a loadMovie action.
var loadMovie will work because you have specified that your using the name as a variable.
I do not advise using actionscript syntax as variable names though (it becomes quite confusing). But its always good practice to put var in front of a variable when declaring it.