tutorials flex difference_scope_as2_as3
difference scope as2 as3
Article written the 06/04/2008 20:17.
By ALCARAZ Marc ( aka eKameleon ).
The problem
Here is a quick tutorial to illustrate a small difference (bug?) between portée des variables in ActionScript 3 compared to portée des variables in AS2.
I’m not going to do a complete re-cap on the use of variable scope in ECMAScript (JS or ACE) but I remembered a problem with the ‘importability’ of a piece of code between AS2 and AS3. I therefore did a little test to isolate the problem.
Example with simple variable scope in AS2.
var a:Number = 1 ; var test:Function = function():Void { trace("> first : " + a) ; // 1 var a:Number = 2 ; trace("> last : " + a) ; // 2 } test() ; // output // > first : 1 // > last : 2
Let’s analyse the example above before moving on to an example in AS3 ;)
In the first test, I am just looking at the scope of the variable “a” inside the test function. When I call the test () function, the first instruction in the function tries to recover the value of a variable which carries the name “a”… This variable does not exist locally in the function (variable declared with the keyword var in the function) and there is no parameter in the function with the name “a”. The script therefore will search outside the function (at the place where it is declared) to see if there is a variable called “a” and inevitably it finds an address in memory with this name and it sends back the value: 1
All is good up to here :)
Then I define a local variable with the keyword var in the function test and then if I call the variable “a” again the value found by script will be that of the local variable: 2
It should be noted in the example above that if I add the argument “a” in the definition of the function and then I pass a value in the parameters of the function at the same time as calling it, I get:
var a:Number = 1 ; var test:Function = function( a:Number ):Void { trace("> first : " + a) ; // 3 var a:Number = 2 ; trace("> last : " + a) ; // 2 } test(3) ; // output // > first : 3 // > last : 2
In the example above the variable scope is limited to a value found in the parameters of the function, the first value of “a” is 3, the value passed in the function at the time of calling it. So no problem there ;)
The same example in AS3 – is there a problem?
var a:Number = 1 ; var test:Function = function():void { trace("> first : " + a) ; // undefined var a:Number = 2 ; trace("> last : " + a) ; // 2 } test() ; // output // > first : undefined // > last : 2
Contrary to the preceding example in AS2, the AS3 code does not manage to recover the value of the variable located outside the function.
On the other hand if I modify the preceding code slightly:
var a:Number = 1 ; var test:Function = function():void { trace("> first : " + a) ; // 1 // var a:Number = 2 ; trace("> last : " + a) ; // 1 } test() ; // output // > first : 1 // > last : 1
If I deactivate (using comments) the declaration of a local variable in the function with the keyword var, I can finally reach the value of my variable declared outside the function with the scope.
Conclusion
In the end, in AS3 it is impossible to access a variable scope located outside a function if this variable is declared later in the function locally with the keyword “var”.
This type of case is very rare if one correctly uses the local variables within a scope.
I wonder if Adobe wants this comportment in AS3 compared to AS2?
A small test in Javascript 1.7 (with JSDB), but also in Javascript 1.5 with Flash Media Server and its SSAS showed me that the problem came from the virtual machine AVM1 of AS1/AS2 and not from AS3.
Here is the Javascript text code:
var a = 1 ; var test = function() { trace("> first : " + a) ; // undefined var a = 2 ; trace("> last : " + a) ; // 2 } test() ; trace("---") ; var a = 1 ; var test = function() { trace("> first : " + a) ; // 1 //var a = 2 ; trace("> last : " + a) ; // 1 } test() ;
In any case, Adobe do seem to have corrected the bug in AS1 et AS2.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


