Mar*_*ram 2 apache-flex flex3 actionscript-3
如果我有一个方法如:
private function testMethod(param:string):void
{
// Get the object that called this function
}
Run Code Online (Sandbox Code Playgroud)
在testMethod中,我可以找出一个叫做我们的对象吗?例如
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?
}
}
Run Code Online (Sandbox Code Playgroud)
对不起答案是否定的(见下面的编辑).函数接收到一个特殊的属性arguments
,在AS2中它曾经拥有的属性caller
大致可以达到你想要的效果.尽管在AS3中仍然可以使用arguments对象,但是从AS3(以及Flex 3)中删除了调用者属性,因此没有直接的方法可以执行您想要的操作.还建议您使用[... rest参数](http://livedocs.adobe.com/flex/3/langref/statements.html#..._ (rest) _parameter)语言功能而不是参数.
编辑:进一步的调查表明,有可能获得当前执行函数的堆栈跟踪,所以如果你很幸运,你可以做一些事情.有关详细信息,请参阅此博客文章和此论坛帖子.
博客文章的基本思想是抛出一个错误,然后立即捕获它,然后解析堆栈跟踪.丑陋,但它可能对你有用.
来自博客文章的代码:
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() + ']' : ''));
Run Code Online (Sandbox Code Playgroud)