开发者

AS3 - Can I have access to the object (or function) who call me?

开发者 https://www.devze.com 2022-12-22 19:42 出处:网络
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:

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 开发者_JS百科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


"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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号