Actionscript duplicate variable definition
if(true) {
var a:int = 1;
}
else {
var a:int = 2;
}
In the above actionscript code I get duplicate variable definition
error because "a" has been declared twice. But, doesn't "a" exist in two "different" scopes?Is there an elegant way of removing this warning, rather
than pulling "a" out 开发者_Go百科of both the scopes and moving it outside the if block?I need that alternate solution since it is in just too many places
in a code which I have to refactor.No, as JavaScript, ActionScript has only one possible scope - function. {}
does not create new scope, only function() {}
does.
yup, all you need to do is
if(true) {
var a:int = 1;
}else {
a= 2;
}
and your error will be gone
There is no elegant solution, other than pulling out the variable or the second time just referencing the variable, not declaring it.
Check out this answer as well:
want to remove warnings:
You should always declare your variables before calling them within the function, always a safer way to code without running into duplication errors.
var a:int;
if(true) {
a= 1;
}
else {
a= 2;
}
Good Luck
精彩评论