In ActionScript 2, variables defined within a class's body were defined in the class's prototype object. This was an efficient means to handle variable definitions but it also meant that complex data types (such as Arrays) had a tendency to be "shared" among instances if not explicitly defined for instances within the class constructor. This is no longer a problem with ActionScript 3. Class variables defined in the class body are now unique to each class instance so this problem no longer occurs.
在AS2中,复杂数据类型(比如数组),如果在类体之中,方法之外直接初始化,而不在构造器中初始化,会被理解为“共享”的数据,而被所有的实例共享使用(类似于static的效果)。但是在AS3中不存在这个问题。类变量是每个实例独有的,唯一的变量。如下:
class myClass{
private var list:Array = [1,2,3]; //在AS2中会有一定问题,AS3中就很好
public function myClass(){
//在AS2中,应该在这里初始化list
}
}
private var list:Array = [1,2,3]; //在AS2中会有一定问题,AS3中就很好
public function myClass(){
//在AS2中,应该在这里初始化list
}
}

收藏到QQ书签