gpt4 book ai didi

actionscript-3 - Flex 3 方法可以检测调用对象吗?

转载 作者:行者123 更新时间:2023-12-04 04:29:55 24 4
gpt4 key购买 nike

如果我有这样的方法:

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

在 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?
}
}

最佳答案

抱歉,答案是否定的(请参阅下面的编辑)。函数收到一个名为 arguments 的特殊属性,在 AS2 中,它曾经具有属性 caller ,它可以大致执行您想要的操作。虽然 arguments 对象在 AS3 中仍然可用,但 caller 属性已从 AS3(因此从 Flex 3)中删除,因此没有直接的方法可以做你想做的事。还建议您使用 [...rest parameter]( http://livedocs.adobe.com/flex/3/langref/statements.html#..._(rest)_parameter) 语言功能而不是 arguments

这是一个reference on the matter (搜索 callee 以查找相关详细信息)。

编辑:进一步的调查表明,可以获取当前执行函数的堆栈跟踪,所以如果幸运的话,您可以用它做一些事情。参见 this blog entrythis forum 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() + ']' : ''));

关于actionscript-3 - Flex 3 方法可以检测调用对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/194733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com