Debugger在构造类的实例前先调用getter methods

Debugger在构造类的实例前先调用getter methods

Time:2004-11-23
Environment:Macromedia Flash MX Professional 2004 7.2
ActionScript version:2.0

在ActionScript中,用new constructorFunction()创建一个class的instance。Debugger在调用constructorFunction之前,首先调用class中定义的所有getter method,如果getter method中出现new constructorFunction(),则会导致无限循环。
//Vector2D.as
class Vector2D {
private var m_x:Number;
private var m_y:Number;
public function Vector2D(x:Number, y:Number) {
trace("The constructor is called!");
}
public function get inverse():Vector2D {
trace("inverse is accessed!");
return new Vector2D(1/m_x, 1/m_y);
//return null;
}
}

//fla
import Vector2D;
var v1:Vector2D = new Vector2D(1,2);
Ctrl + Enter测试,很正常
Output : The constructor is called!
Ctrl + Shift + Enter调试,引起无限循环调用,递而不归。
Output :
inverse is accessed!
… …
inverse is accessed!
inverse is accessed!
256 levels of recursion were exceeded in one action list.
This is probably an infinite loop.
Further execution of actions has been disabled in this movie.

修改类文件,在inverse中不再创建Vector2D的实例:
//Vector2D.as
class Vector2D {
private var m_x:Number;
private var m_y:Number;
public function Vector2D(x:Number, y:Number) {
trace("The constructor is called!");
}
public function get inverse():Vector2D {
trace("inverse is accessed!");
//return new Vector2D(1/m_x, 1/m_y);
return null;
}
}
Ctrl + Shift + Enter调试,可以看到,inverse首先被调用,然后才是constructor!
Output :
inverse is accessed!
The constructor is called!

脱离了debugger则很正常。
不要在getter method中实例化自己!
实在没时间研究为什么了,郁闷,每次用flash认真做东西,总会发现一两个bug。

Advertisements
This entry was posted in Flash and ActionScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s