从函数内部获取var以将其返回AS3
我有此静态公共功能berechne()
,我希望此功能返回data
。但是数据
在另一个功能内部,因此我无法在功能之外访问它以返回它。我也不能让第二个函数离开,因为有必要在event.complete
之后接收获取的数据。
如何返回数据
?
Berechne():
static public function berechne(value1: String): Object {
var request: URLRequest = new URLRequest('http://localhost/whatever.json');
var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, responseReceived);
loader.load(request);
function responseReceived(event: Event): void {
var data:Object = JSON.parse(loader.data); //I would like to return this ...
}
return data; //... here
}
谢谢!
I have this static public function berechne()
and I want this function to return data
. But data
is inside another function, so I can't access it outside the function to return it. I also can't let the second function away, because it is necessary to receive the fetched data after Event.COMPLETE
.
How can I return data
?
berechne():
static public function berechne(value1: String): Object {
var request: URLRequest = new URLRequest('http://localhost/whatever.json');
var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, responseReceived);
loader.load(request);
function responseReceived(event: Event): void {
var data:Object = JSON.parse(loader.data); //I would like to return this ...
}
return data; //... here
}
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试此设置:
另一种方法是避免返回零件,而是使用该函数直接更新对象(作为函数参数)。
在此示例中,
您应该通过返回更新的对象现在以参数
value2
:You can try this setup:
Another way is to just avoid the Return part and instead use the function to update your Object directly (as a function parameter).
In this example,
Your Object that should be updated by a return is now passed in as a parameter
value2
:您必须以某种方式围绕
loader.load()
中涉及的异步行为进行处理,否则您在调用berechne()
时就无法检索“数据”。使用该数据
的代码应首先检查数据是否实际上是加载的,如果没有,则应以某种方式保释。我会这样:You have to somehow work around the asynchronous behavior involved in
Loader.load()
, otherwise you just cannot retrieve your "data" when yourberechne()
is called. The code that uses thatdata
should either first check if the data was actually loaded, then if not, bail out somehow. I would do like this: