gskinner.com: gBlog: FlashMX 2004 source code, news, reviews and opinions
Back to gBlog Main gskinner.com website: source code, portfolio, etc. contact Grant Skinner

Syndication
RSS 1.0
RSS 2.0


Subscribe
Enter your email address to be notified when posts are added.

Search

Resources
Conference Session Notes

Flash Blogs
Waxpraxis
Philterblog
W3Blog
Jonas Galvez
Josh Dura
Quasimondo
Flashguru
Sean Voisen
Colin Moock
Flazoom
Greg Burch
Pope De Flash
Peter Hall
Glyn Thomas
actionscript.com
Princess Pegg


Aggregators
Flog
FullAsAGoog
MXNA
Hall of Justhese


« 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:

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

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

Posted by: Andreas Weber at October 28, 2003 12:32 AM


Hosting by NetKeepers.ca | Powered by Movable Type 2.661
The text content of this blog is licensed under a Creative Commons License. Graphics are ©2003 Grant Skinner.