从函数内部获取var以将其返回AS3

发布于 2025-01-27 19:48:10 字数 701 浏览 4 评论 0原文

我有此静态公共功能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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2025-02-03 19:48:10

您可以尝试此设置:

static public function berechne(value1: String): Object 
{
    var request: URLRequest = new URLRequest('http://localhost/whatever.json');
    var loader: URLLoader = new URLLoader();

    var data:Object; //# define outside of function
     
    loader.addEventListener(Event.COMPLETE, responseReceived);
    loader.load(request);

    function responseReceived(event: Event): Object //# set Object as return type
    {
        data = JSON.parse(loader.data); //I would like to return this ...
        return data; //... here
    }
   
}

另一种方法是避免返回零件,而是使用该函数直接更新对象(作为函数参数)。

在此示例中,
您应该通过返回更新的对象现在以参数 value2

static public function berechne(value1: String, value2: Object): void
{
    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
    { value2 = JSON.parse(loader.data); }
   
}

You can try this setup:

static public function berechne(value1: String): Object 
{
    var request: URLRequest = new URLRequest('http://localhost/whatever.json');
    var loader: URLLoader = new URLLoader();

    var data:Object; //# define outside of function
     
    loader.addEventListener(Event.COMPLETE, responseReceived);
    loader.load(request);

    function responseReceived(event: Event): Object //# set Object as return type
    {
        data = JSON.parse(loader.data); //I would like to return this ...
        return data; //... here
    }
   
}

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:

static public function berechne(value1: String, value2: Object): void
{
    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
    { value2 = JSON.parse(loader.data); }
   
}
清浅ˋ旧时光 2025-02-03 19:48:10

您必须以某种方式围绕loader.load()中涉及的异步行为进行处理,否则您在调用berechne()时就无法检索“数据”。使用该数据的代码应首先检查数据是否实际上是加载的,如果没有,则应以某种方式保释。我会这样:

class Your {
  static private Object data=null;
  static private Boolean dataLoaded=false;
  static private String dataJson="";
  public static function getData():Object {
    if (!Your.dataLoaded) return null;
    return Your.data;
  }  
  public static function berechne():void // note it's void, you can't expect data to be returned right away
  {
    if (Your.dataJson!=""){ //perhaps you've already loaded the data?
      Your.data=JSON.parse(Your.dataJson); // reset with preloaded data
    } else {
      // else just load. Or ignore that "if" and plain load, perhaps you want new values in your program
      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
      { 
        Your.dataJson= event.target.data; // you have no "loader" here!
        Your.data=JSON.parse(Your.dataJson);
        Your.dataLoaded=true; // set flag "data ready"
      }
    }
  } // end berechne
} // end class

You have to somehow work around the asynchronous behavior involved in Loader.load(), otherwise you just cannot retrieve your "data" when your berechne() is called. The code that uses that data should either first check if the data was actually loaded, then if not, bail out somehow. I would do like this:

class Your {
  static private Object data=null;
  static private Boolean dataLoaded=false;
  static private String dataJson="";
  public static function getData():Object {
    if (!Your.dataLoaded) return null;
    return Your.data;
  }  
  public static function berechne():void // note it's void, you can't expect data to be returned right away
  {
    if (Your.dataJson!=""){ //perhaps you've already loaded the data?
      Your.data=JSON.parse(Your.dataJson); // reset with preloaded data
    } else {
      // else just load. Or ignore that "if" and plain load, perhaps you want new values in your program
      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
      { 
        Your.dataJson= event.target.data; // you have no "loader" here!
        Your.data=JSON.parse(Your.dataJson);
        Your.dataLoaded=true; // set flag "data ready"
      }
    }
  } // end berechne
} // end class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文