View Full Version : declaring variables.
kO2n
September 2nd, 2003, 07:09 AM
Whats the difference with declaring variables in the following manner:
i = 0;
and
var i = 0;
Regards,
Viru.
andr.in
September 2nd, 2003, 07:13 AM
There isn't.. at least that's what I think! I never use var!
λ
September 2nd, 2003, 07:34 AM
well... it does have one difference. If you use it in a function:
function addOne(){
var i;
i+=1;
return i;
}
//Once the function finishes, i is destroyed. This is what the var keyword does in a function.
function addOne2(){
i+=1;
return i;
}
//because we omitted var, i is not destroyed:
for(var j = 0;j < 3;j++){
trace(addOne2());
}
//outputs 123.
for(var j = 0;j < 3;j++){
trace(addOne());
}
//outputs 111, as i is always destroyed
Other than that, it's just good form and helps when you move on to C or Java.
kO2n
September 2nd, 2003, 07:37 AM
Yeah i do Java too.
It says in the actionscript dictionary that it makes the variable local when var is used. Makes sense, i just wondered thats all.
Thanks,
Viru.
senocular
September 2nd, 2003, 08:15 AM
local to function calls. Theres no difference in using var or not on the timeline.
[m]
September 2nd, 2003, 08:36 AM
There is, but a very slight one. With var you have to wait a frame before you can access it from another place (like another mc), if you don't use var you can access it straight away.
pom
September 2nd, 2003, 01:37 PM
There's something in the AS tricks about that and [m], are you sure about what you're saying? It sounds strange :-\ How would you access that local variable from another movieclip?? Can you give an example?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.