4

I've asked this same question with Python.

Now I like to know if this can be done in AS3.

If I have something like this:

package
{
    public class SomeClass
    {
        private function A():void { C() }
        private function B():void { C() }

        private function C():void
        {
            // who is the caller, A or B ???
        }

        public function SomeClass()
        {
            A()
            B()
        }
    }
}

Despite the design or other issues, this is only a question of an inquiring mind.

Note: I like to have an access to an instance of the caller function so I can call that caller function (if I want to)

Note 2 : This has to be done without changing function C() signature

Community
  • 1
  • 1
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83

2 Answers2

5

"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."

From http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/arguments.html

That's the only way you can do that, otherwise you'll need to make a global variable to tell what function is calling C

1

Sure it can be done. You can do something like

private function C():void
{
  var e:Error = new Error();
  var stack:String = e.getStackTrace();
  //analyze stack and find out which function called it.
}

this is ugly but it would work.

Erix
  • 7,059
  • 2
  • 35
  • 61
  • Yes, I know this, but read Note: "I like to have an instance of the caller function" and using the stack I only have the name of the function. Not the object or the function object – Lucas Gabriel Sánchez Mar 15 '10 at 15:33
  • you might be able to get that once you have the name. look here: http://www.actionscript.org/forums/showthread.php3?t=197943 – Erix Mar 15 '10 at 15:52
  • Still the same, that way I can acces the method name, but what if that function is public and is called by other object? How can I call the function over the object who call me? To do that I need the function instance or the object (owner of the function) instance and the function name. – Lucas Gabriel Sánchez Mar 15 '10 at 17:19