tags:

views:

1816

answers:

5

If I have a method such as:

private function testMethod(param:string):void
{
  // Get the object that called this function
}

Inside the testMethod, can I work out what object called us? e.g.

class A
{
  doSomething()
  {
    var b:B = new B();
    b.fooBar();
  }
}

class B
{
  fooBar()
  {
    // Can I tell that the calling object is type of class A?
  }
}
+5  A: 

Sorry the answer is no (see edit below). Functions received a special property called arguments and in AS2 it used to have the property caller that would do roughly what you want. Although the arguments object is still available in AS3 the caller property was removed from AS3 (and therefore Flex 3) so there is no direct way you can do what you want. It is also recommeded that you use the ...rest parameter language feature instead of arguments.

Here is a reference on the matter (search for callee to find the relevant details).

Edit: Further investigation has shown that it is possible to get a stack trace for the current executing function so if you are lucky you can do something with that. See this blog entry and this forum post for more details.

The basic idea from the blog post is you throw an Error and then catch it immediately and then parse the stack trace. Ugly, but it may work for you.

code from the blog post:


var stackTrace:String;

try { throw new Error(); }
catch (e:Error) { stackTrace = e.getStackTrace(); }

var lines:Array = stackTrace.split("\n");
var isDebug:Boolean = (lines[1] as String).indexOf('[') != -1;

var path:String;
var line:int = -1;

if(isDebug)
{
    var regex:RegExp = /at\x20(.+?)\[(.+?)\]/i;
    var matches:Array = regex.exec(lines[2]);

    path = matches[1];

    //file:line = matches[2]
    //windows == 2 because of drive:\
    line = matches[2].split(':')[2];
}
else
{
    path = (lines[2] as String).substring(4);
}

trace(path + (line != -1 ? '[' + line.toString() + ']' : ''));
James Fassett
Hmm, good answer. I'm not sure I like the idea of throwing and catching an error like that. I might just have to pass a "thisObject" as a parameter to my function (it's so I can use the Function.call() method).
Mark Ingram
+1  A: 

I'd second the idea of explicitly passing a "callingObject" parameter. Unless you're doing really tricky stuff, it should be better for the caller to be able to supply the target object, anyway. (Sorry if this seems obvious, I can't tell what you're trying to accomplish.)

aib
+1  A: 

To add to the somewhat ambiguous first paragraph of James: the arguments property is still available inside a Function object, but the caller property has been removed.

Here's a link to the docs: http://livedocs.adobe.com/flex/3/langref/arguments.html

Christophe Herreman
Thanks for the tip - I've updated my answer to include that information
James Fassett
+3  A: 

Is important to know that stackTrace is only available on the debugger version of Flash Player. Sorry! :(

+1  A: 

This might help someone, I'm not sure... but if one is using an Event this is possible using the e.currentTarget as follows:

private function button_hover(e:Event):void { e.currentTarget.label="Hovering"; }