# Interesting scope issue

**URL:** <https://forum.kirupa.com/t/interesting-scope-issue/262592>\
**Category:** flash\
**Created:** [June 7, 2008, 12:08pm UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592 "2008-06-07T12:08:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tecknoize](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@tecknoize](https://forum.kirupa.com/u/tecknoize)\
**Post date:** [June 7, 2008, 12:08pm UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592/1 "2008-06-07T12:08:30Z")

</div>

Hi,  
I’m working on a Debug class which will help me to do certain thing for debugging purpose (duh). However, I’ve ran into an interesting scope issue :

```auto

public static function trace(str:String):void
{
        trace(str);
}

```

At first, I didn’t see what was the problem, and I’m not sure why ( maybe because I use trace too much ). Anyway, it’s pretty obvious : the trace statement inside the function will call that function back again and again, because the “trace” name override the one of the flash API.

I’m not really asking for a solution. I mean, I could just rename my custom trace and that would do it. What I was wondering is how could I call the original trace, or any overridden name inside a scope?

Here’s a more explicit example :

```auto

var a:Number = 4;
function foo():void
{
     var a:Number = 12;
     trace(a);
}

```

This code will print 12. But what if I want to print 4, the value of the global “a” variable?

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [June 7, 2008, 12:16pm UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592/2 "2008-06-07T12:16:30Z")

</div>

create your own namespace for it…

---

<div class="post-metadata">

**Author:** ![tecknoize](https://avatars.discourse-cdn.com/v4/letter/t/a88e57/32.png) [@tecknoize](https://forum.kirupa.com/u/tecknoize)\
**Post date:** [June 7, 2008, 12:46pm UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592/3 "2008-06-07T12:46:55Z")

</div>

That would work for the “a” example, but for a function inside a class, it doesn’t, because I can’t create a definition outside the class scope. However, a “private class” seams to do the trick :

```auto

class Trace
{
    public static const func:Function = trace;
}

```

And then I can use it like this :

```auto

public static function trace(...args):void
{
    Trace.func.apply(null, args);
}

```

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [June 8, 2008, 11:04am UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592/4 "2008-06-08T11:04:57Z")

</div>

```auto
trace(this.a)

```

may do it

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [June 8, 2008, 2:56pm UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592/5 "2008-06-08T14:56:25Z")

</div>

Not sure I completely understand, but maybe this will help clear up scope… Or make it worse…

[AS]package {

```
import flash.display.Sprite;

public class NSTest extends Sprite {

    public namespace obo = "http://www.onebyonedesign.com/";

    private var a:int = 4;

    public function NSTest():void {
        var a:String = "This is a string.";
        trace (a); // This is a string.
        obo::trace(this.a); // called from obo namespace: 4
    }

    obo function trace(obj:*):void {
        trace ("called from obo namespace: " + obj);
    }
}

```

}[/AS]

---

<div class="post-metadata">

**Author:** ![wvxvw](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@wvxvw](https://forum.kirupa.com/u/wvxvw)\
**Post date:** [June 9, 2008, 12:52am UTC](https://forum.kirupa.com/t/interesting-scope-issue/262592/6 "2008-06-09T00:52:39Z")

</div>

The problem is that if you want to substutute your trace() function for the default one you wan’t be able to import it… i.e. import trace; will be ignored by compiler because it already imported it before it even looked into your classes…  
Also, I couldn’t find which namespace this function belongs to… and it’s not AS3 namespace ([http://www.adobe.com/2006/builtin](http://www.adobe.com/2006/builtin)) i.e. calling AS3::trace() gives error…
