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?
There are two ways to define a function in ActionScript 3.0: you can use a function statement or a function expression. The technique you choose depends on whether you prefer a more static or dynamic programming style. Define your functions with function statements if you prefer static, or strict mode, programming.
ActionScript makes it possible for developers to create onscreen environments (such as games, tutorials, and e-commerce applications) that can respond to user input through the keyboard or mouse. ActionScript is an event-based language: just as is the case in real life, actions are triggered by events.
Action Script 3.0 in Flash allows you to create all kinds of fully interactive applications such as dynamic websites and computer games. Also, AS3. 0 is an object-oriented programming language; if you are familiar with AS3. 0, it will help you to learn other object-oriented language such as Javascript.
It means that it doesn't return any data By default Flash always expect to return a value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With