 |
|
 |
« Update fix (sorta) |
Back to Main
| Third party components? Not legally. »
 |
October 27, 2003 |
 |
|
IMPORTANT NOTE: This is an old archive. It is only here to support outdated external links. To view the updated version of this archive, please go to the blog index, and search for the title of this document using the search form.
|
Function scope in temporary vars
Posted by Grant
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:
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:
class TestClass {
var f1:Function;
function test1() {
f1 = test2;
var f2:Function = test2;
f1();
f2();
}
function test2() {
trace("this: "+this);
}
}
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. :)
Posted @ 11:55 PM by Grant
| TrackBack
Comments
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
|
 |