Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript: Why is it possible to assign a variable before it is declared?

inspired by the question int a[] = {1,2,}; Weird comma allowed. Any particular reason? I remembered a question concerning the syntax in Adobe's Actionscript.

For some reason it is possible (at least in Flex 3) to assign a value to a variable before it was declared:

 public function foo() : void {
      a = 3;
      var a : int = 0;
 }

Does this make any sense..? Is this a bug in the Adobe FlexBuilder compiler? Or is this due to maybe some legacy to older Ecmascript editions?

like image 474
Mister Henson Avatar asked Aug 12 '11 19:08

Mister Henson


1 Answers

An interesting implication of the lack of block-level scope is that you can read or write to a variable before it is declared, as long as it is declared before the function ends. This is because of a technique called hoisting , which means that the compiler moves all variable declarations to the top of the function. For example, the following code compiles even though the initial trace() function for the num variable happens before the num variable is declared...

Actionscript 3.0 Docs - Variables (quote found about 2/3 down the page)

like image 186
Sam DeHaan Avatar answered Nov 09 '22 23:11

Sam DeHaan