Anonymous function scope disaster!

I have 2 examples to show…

#1 (works)…


var i:int = 3;
var f:Function = function () {
  var i:int = 5555;
  trace(i); // traces '5555'
}
trace(typeof f); // traces 'function'
f();

#2 (problem)…


var i:int = 3;
var f:Function;
f = function () {
  var i:int = 5555; // gives ReferenceError: Error #1056: Cannot create property ::i on global.
  trace(i); // this line is never reached
}
trace(typeof f); // traces 'function'
f();

Notice the comments in #2. Why doesn’t #2 work? Look closely and see, the only difference is that I moved the assignment of ‘f’ to the next line, instead of the same line where it is declared as being a Function. But for some reason, f() in #2 thinks its own scope exists in the place where “var i:int=3” is declared, so the function fails and brings Flash to its knees!!! I need #2 to work, because I assign anonymous functions to an Object instance that I pass around. It is the style of coding I have used in Flash for years

I thought AS 3.0 allowed anonymous functions as a genuine courtesy to the AS 1/2 crowd to ease the transition to its forced 100% external class-based stuff. But I am worried I have encountered a bug that delivers a fatal blow to my coding style … ???

Someone please show me I am wrong…