1

I'm trying to write debugging tools and I would like to be able to get the class name of the caller. Basically, caller ID.

So if I have a method like so, I want to get the class name:

public function myExternalToTheClassFunction():void {
   var objectFunction:String = argument.caller; // is functionInsideOfMyClass
   var objectFunctionClass:Object = argument.caller.this;
   trace(object); // [Class MyClass]
}

public class MyClass {

   public function functionInsideOfMyClass {
       myExternalToTheClassFunction();
   }
}

Is there anything like this in JavaScript or ActionScript3? FYI AS3 is based on and in most cases interchangeable with JS.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

2 Answers2

1

For debugging purposes you can create an error then inspect the stack trace:

var e:Error = new Error();
trace(e.getStackTrace());

Gives you:

 Error
     at com.xyz::OrderEntry/retrieveData()[/src/com/xyz/OrderEntry.as:995]
     at com.xyz::OrderEntry/init()[/src/com/xyz/OrderEntry.as:200]
     at com.xyz::OrderEntry()[/src/com/xyz/OrderEntry.as:148]

You can parse out the "caller" method from there.

Note that in some non-debug cases getStackTrace() may return null.

Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
0

Taken from the documentation:

Unlike previous versions of ActionScript, ActionScript 3.0 has no arguments.caller property. To get a reference to the function that called the current function, you must pass a reference to that function as an argument. An example of this technique can be found in the example for arguments.callee.

ActionScript 3.0 includes a new ...(rest) keyword that is recommended instead of the arguments class.

Try to pass the Class name as argument:

Class Code:

package{

    import flash.utils.getQualifiedClassName;

    public class MyClass {

        public function functionInsideOfMyClass {

            myExternalToTheClassFunction( getQualifiedClassName(this) );

        }

    }

}

External Code:

public function myExternalToTheClassFunction(classname:String):void {

    trace(classname); // MyClass

}
ElChiniNet
  • 2,778
  • 2
  • 19
  • 27
  • FYI I tried passing `this` to the static method and that threw errors as well. It doesn't seem to throw any compiler errors if I create a variable, `var object:Object = this;` and pass `object` but didn't run it. – 1.21 gigawatts Jan 02 '16 at 09:08
  • 1
    Yes, this is the expected behaviour. Take a look at this link and search the static method part. http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f30.html – ElChiniNet Jan 02 '16 at 11:47