在使用,效率或背景技术之间是否有差异
var mc:MovieClip = MovieClip(getChildByName("mc"));
和
var mc:MovieClip = getChildByName("mc") as MovieClip;
?
选择只是公约,偏好的问题,还是有不能使用的情况?
解决方法
This article描述了差异:
A key difference between casting and the as operator is the behavior
on failure. When a cast fails in ActionScript 2,null is returned.
When a cast fails in ActionScript 3,a TypeError is thrown. With the
as operator in ActionScript 3,whenever a cast fails the default value
for the datatype is returned.
因为也允许你转换为Array,这是以前不可能的,因为转换函数Array()优先。
编辑:关于性能,使用报告比在各种文章中的函数调用风格投射更快:[1] [2] [3]。引用的第一篇文章介绍了深度的性能差异,并报告说,速度是4倍4.5倍。
编辑2:在正常情况下,速度不仅仅是4x-4.5x,而是在try-catch块中包装(转换)样式转换,并且实际上最终被抛出,更像30x – 230x更快。在AS3中,如果你认为你会做一些异常的事情(那可能会引发一个错误),那么很明显你应该在你飞跃之前看看。除非被API强制使用,否则绝对不要使用try / catch,并且这意味着永远不会(cast)即使没有异常被抛出,也可以查看try / catch的性能影响。即使在没有什么问题的情况下,设置一个try / catch块也有一个性能损失。