Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript - Obtain the name of the current function

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?

like image 213
Fragsworth Avatar asked Jan 19 '11 05:01

Fragsworth


People also ask

How will you declare a function in ActionScript?

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.

What is ActionScript used for?

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.

What is ActionScript 3. 0?

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.

What does void in ActionScript 3.0 indicate?

It means that it doesn't return any data By default Flash always expect to return a value.


2 Answers

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.

like image 157
weltraumpirat Avatar answered Sep 21 '22 17:09

weltraumpirat


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.

like image 24
merv Avatar answered Sep 19 '22 17:09

merv