I just smacked into an interesting phenomenon with function scope in temporary variables, that appears to be new in F04.
When you call a function that has been assigned to a temporary variable in F04 (a variable declared with var in an execution block), it’s scope is, well… uh, it appears to be non-existant… it has it’s own little scope, in some distant galaxy, far, far away. While this sort of makes sense, it wasn’t what I was expecting.
This occurs in FLAs published to AS1 and AS2 in F04, but does not seem to occur in FMX. It would be interesting to know what exactly has changed, and why.
Try this out in a new FLA in F04:
// first frame: f4 = function() { var f5 = function() { trace(this); } f5(); } f4(); |
If you run this in F04, it traces ‘undefined’. However, if you run it in FMX, it traces ‘[Object, Object]’. Of course, this phenomenon also expresses itself in classes, which is where I ran into it:
// in TestClass.as: class TestClass { var f1:Function; function test1() { f1 = test2; var f2:Function = test2; f1(); f2(); } function test2() { trace("this: "+this); } } // in frame one of a new FLA: var test:TestClass = new TestClass(); test.test1(); |
The above code will trace:
this: [object Object]
this: undefined
The first call executes within the class scope, because the f1 variable is a static member of the TestClass instance. The second call traces undefined, because it is assigned to a temporary variable with its own little scope.
Interesting…
Hopefully this saves someone else a little time in debugging. 🙂
Interesting find!
Flash seems to have a _very_ peculiar way of handling function declarations inside curly braces.
Here’s another example:
http://chattyfig.figleaf.com/ezmlm/ezmlm-cgi/1/92112
http://www.peterjoel.com/blog/index.php?archive=2003_10_26_archive.xml#106771351248129644
nested function scopes
I was leaving a comment on Grant’s blog, but it started getting bit long so it’s here instead