开发者

Receiving data from a loader in Actionscript

This is something I noti开发者_高级运维ced on the Adobe documentation pages.

When receiving a textfile from a URL, the function to set the text will either look like this:

function completeHandler(event:Event):void {
    var txt:String = URLLoader(event.currentTarget).data as String;
    tf.text = txt;
    ...
}

Or this:

function completeHandler(event:Event):void {
    tf.text = URLLoader(event.target).data;
    ...
}

Myself, I've always done this:

function completeHandler(event:Event):void {
    tf.text = event.target.data;
    ...
}

So my question is: is there an important difference I should be aware of?


The only possible, beneficial difference between the three is casting. By casting the result of the loaders data object, you're basically doing two things: forcing a type onto the data and thus essentially performing a type-check and secondly you're explicitly telling the virtual machine what kind of data it's dealing with, instead of making it figure it out itself. The second point, there could be performance gain to this. As for the first point about casting and thus type-checking, allow me to provide a scenario. Say you're loading an XML file. Perhaps all you want to do is show the XML in a text field as you have done here, but you want to ensure that it is valid XML. You would explicitly cast the result as XML inside a try/catch statement, like so:

function completeHandler(event:Event):void {
   var res:XML;

   try{
      res = new XML(e.currentTarget.data);
      tf.text = res.toString();
   }catch(e:Error){
      //Do something or nothing about this error
      tf.text = "You have attempted to open an XML file containing mal-formed XML data.";
   }    
}

So as you can see it comes down to more than a matter of personal choice. Performance and just plain programming in good practice come into play. Actionscript 3 is a strictly-typed language for a reason: because strict typing ensures proper function as well as enables optimization/better/cleaner execution. One fact to testify to this is the actionscript 2, non-strict type virtual machine, which runs about 20X slower than the AVM2 (actionscript 3 vm).


If all three methods work perfect and they do what you need them to do, then there is no significant difference, it's just type casting - go with the one which is easier for you. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜