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 :
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 :
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?
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 :
class Trace
{
public static const func:Function = trace;
}
And then I can use it like this :
public static function trace(...args):void
{
Trace.func.apply(null, args);
}
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);
}
}
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) i.e. calling AS3::trace() gives error…