0

What can be the way to know the class name "Abc" from where the function of a reference variable "def" has been called ? Except these two ways :

1) Adding the def as a child of Abc

2) Injecting reference of Abc into Def prior or during the function call.

here is the class Abc :

public class Abc
{
   var def:Def ; 
   public function Abc()
   {

         def = new Def();
        def.myfun() ;
   }



}


public class Def 
{

  public function Def()
  {
  }
  public function myfun():void 
  {

     // Code to know, this function has been called from class named Abc ; 
  }

}
Vishwas
  • 1,533
  • 2
  • 19
  • 40

3 Answers3

1

No, that is not possible. It is also useless. The closest thing you can do to what you want is to get a reference to the function calling Abc.myfun() via arguments.callee:

public function myfun():void
{
    var callee:Function = arguments.callee;
}

Though this won't have any useful information attached like the function or class name.

Marty
  • 39,033
  • 19
  • 93
  • 162
  • Yes, it seems not possible. But it can be useful, to know the code flow in trace function. Trying to find out some shortcut, such that it does not need any injection or display list addition. – Vishwas Jan 29 '14 at 16:21
0

Maybe like this:

public class Abc
{
   var def:Def;

   public function Abc()
   {
        def = new Def();
        def.myfun( true ) ;
   }
}


public class Def 
{
  public function Def()
  {
  }

  public function myfun(fromAbc:Boolean=false):void 
  {
    if( fromAbc == true )
    {
        // Code to know, this function has been called from class named Abc ;
    }
  }
}
3vilguy
  • 981
  • 1
  • 7
  • 20
0

You could get the information from the stacktrace, but that would only work in debug mode.

There isn't really a clean way to do it, because you shouldn't need to do it. If you explained a bit more about your situation / goal there is probably a simple answer (requiring a small redesign) that will help you. (cant post comments)

VBCPP
  • 152
  • 1
  • 10
  • Was just looking for shortest way to add information about code flow in custom Trace functions. – Vishwas Jan 29 '14 at 16:18
  • If you're only using it in tracing, the debug limitation might be ok for you? Parsing the stacktrace will provide you with the filename of the calling method. http://stackoverflow.com/questions/149073/stacktrace-in-flash-actionscript-3-0 – VBCPP Jan 29 '14 at 16:57