# Anonymous function scope disaster!

**URL:** <https://forum.kirupa.com/t/anonymous-function-scope-disaster/227952>\
**Category:** Uncategorized\
**Created:** [June 4, 2007, 6:30am UTC](https://forum.kirupa.com/t/anonymous-function-scope-disaster/227952 "2007-06-04T06:30:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hypah](https://avatars.discourse-cdn.com/v4/letter/h/e79b87/32.png) [@Hypah](https://forum.kirupa.com/u/Hypah)\
**Post date:** [June 4, 2007, 6:30am UTC](https://forum.kirupa.com/t/anonymous-function-scope-disaster/227952/1 "2007-06-04T06:30:40Z")

</div>

I have 2 examples to show…

#1 (works)…

```auto

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

```

#2 (problem)…

```auto

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…
