A common way to get a reference to the calling method?

I have 2 classes representing 2 objects. From the "whoCalledMe" function, I want to know which object is called a function (without passing this information as an argument). I used the make-ver property, "caller", which would give me the link I'm looking for. Is there a general way to get a link to a caller?

package {
    public class ObjectCallingTheFunction {
        public var IDENTITY:String = "I'm the calling function!";

        public function ObjectCallingTheFunction() {
            var objectWithFunction:ObjectWithFunction = new ObjectWithFunction();
            objectWithFunction.whoCalledMe();
        }
    }
}

package {
    public class ObjectWithFunction {
        public function whoCalledMe ():void {
            trace(caller.IDENTITY); // Outputs: "I'm the calling function!"
        }
    }
}
+3
source share
4 answers

This will help to find out why you need it, because I have a feeling that you really are not doing it. If the method is anonymous, you can associate the 'this' keyword with .apply on the method:

var foo:Function = function(arg:int):void
{
    trace(this);
};

var bar:Object = {
    toString: function():String { return "bar"; }
};

var baz:Object = {
    toString: function():String { return "baz"; }
};

foo.apply(bar); // <-- Prints "bar"
foo.apply(baz); // <-- Prints "baz"

, , , , "this" , , , apply. , "this" , .

, . caller arguments, AS3. arguments.callee, , .

+3

, . , / . , . Flash IDE, Flashbuilder. Google "as3 breakpoints", .

0

Source: https://habr.com/ru/post/1781485/


All Articles