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

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).

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).

1. Very interesting. I didn't know about this.

– TheDarkIn1978 Aug 12 at 23:33.

As far as I know it is the feature of Flash Virtual Machine which declares (allocate memory etc) all the function's variables before function's body execution. So declaring variable somewhere in the function block in ActionScript code just reports compiler to declare variable and it declares at the beginning of the function block at runtime. That's why your code is the same as: public function foo() : void { var a : int = 3; a = 0; } The same reason has the compiler warning when you declare some variable twice in the function's body.

Thanks to both of you! Although it sounds a bit weird to me, I finally know the reason – Mister Henson Aug 12 at 19:52 @Mister Henson, then you should accept an answer, unless you're waiting for something better :) – Peter Aug 12 at 20:50 @Peter: Ah ok I see, thx I'm still learning ;) – Mister Henson Aug 13 at 0:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions