I want to get the name of a function from inside that function. e.g.:
function blah() {
//I want to get the string "blah" here, from the function's name
}
Or at least the Function object?
I want to get the name of a function from inside that function. e.g.:
function blah() {
//I want to get the string "blah" here, from the function's name
}
Or at least the Function object?
Use arguments.callee to get a reference to the current function.
I you want to get the function name, it is a bit trickier: All functions are treated as method closures (pieces of code which can be passed around as an argument), so they do not own a reference to an enclosing class type, nor do they have a "current name".
However, if (and only if) the method is public, and you want to get the method name from the class declaration of an instance object containing the method, you can use describeType:
public function someFunction() : void {
var callee:Function = arguments.callee;
trace (getFunctionName(callee, this)); // ==> someFunction
}
private function someOtherFunction() : void {
var callee:Function = arguments.callee;
trace (getFunctionName(callee, this)); // ==> not found
}
private function getFunctionName (callee:Function, parent:Object):String {
for each ( var m:XML in describeType(parent)..method) {
if ( parent[m.@name] == callee) return m.@name;
}
return "not found";
}
Note that this would not work when you call someFunction()
from a constructor, because the object is not fully instantiated - describeType(this)
in a constructor would cause a compilation error.
I just did it with a stack trace, which is what the Flash Player debugger shows when an unhandled error is thrown. Here's my implementation:
function blah()
{
var e:Error = new Error();
var s:String = e.getStackTrace();
var functionName:String = s.slice(s.indexOf('/')+1, s.indexOf('('));
trace(functionName); //blah
}
You'll probably need to make the parsing of the string a bit fancier, if you want to make this a separate function. But the function name is definitely in the stack trace.
Edit: Important Caveat
The AS3 Lang Ref says that the getStackTrace method only works in the Debugger versions of Flash Player/AIR, and will return null
otherwise. So this is definitely not a deployable solution. Sorry.
You probably wont be able to get the name of a function, because they don't really have a "name" as such. Just like your variables don't have names. They are just pointers to an object.
Getting the name doesn't make sense, partly due to the following example:
function foo():void
{
//get function name.
}
var bar:Function = foo;
bar();
A function is just like any other object/variable. What "name" would this function find? "foo"? or "bar"? Both foo and bar are referencing the same variable of type Function.
A better way to handle this would be to pass something in to the function as an argument:
function foo(myName:String):void
{
trace(myName);
}
You can however, get a reference to the current function using the arguments.callee :)
To pull this off in Actionscript 2, which I had to do recently with some legacy code
function getFunctionName(func:Function):String
{
for(var prop in this)
{
if(this[prop] == func)
{
return prop;
break;
}
}
}
And use it something like this
trace(getFunctionName(arguments.callee));
when i needed IDs for functions for my event system implementation i didn't find a way to access a name
of a non-anonymous function passed as an argument so i had to write a wrapper that stored nothing except the Function
and its ID