很好的理由...嗯...有點複雜,解釋...
好嗎?ActionScript是prototype-oriented,因爲是ECMA-腳本...類只是一個語法糖的引入動作2(這在AS3再次改變,但是這是一個不同的主題)...
,所以如果這是原代碼:
class com.weatherwidget.City {
var zipCode:String;
var forecastText:Array = new Array(5);
}
那麼這就是,到底發生了什麼:
//all classes get stuffed into _global, with packages being a property path:
if (_global.com == undefined) _global.com = {};
if (_global.com.weatherwidget == undefined) _global.com.weatherwidget = {};
//and the actual definition:
_global.com.weatherwidget.City = function() {};
_global.com.weatherwidget.City.prototype = { forecastText:new Array(5) }
的City
原型對象,用作原型的City
實例中,有一個稱爲forecastText
屬性,它是長度爲5的Array
...等等的City
實例仰視forecastText
時,它不能直接找到並將在原型鏈中查找...它會在實例的原型中找到...因此,所有實例共享Array
...
區別在於,第二個示例獲取翻譯爲:
//same thing here:
if (_global.com == undefined) _global.com = {};
if (_global.com.weatherwidget == undefined) _global.com.weatherwidget = {};
//and the actual definition this time:
_global.com.weatherwidget.City = function() { this.forecastText = new Array(5); };
_global.com.weatherwidget.City.prototype = {}
,你可能已經注意到,聲明成員只是一個編譯時的事情......如果沒有被分配給他們,他們根本不會在運行時存在...
好,這explenation要求,你要麼知道的JavaScript或ActionScript 1一點點,但我希望它可以幫助...
格爾茨
back2dos
如果任何人都可以對這個問題提出一個更好的問題名稱請讓我知道。 – Anton 2009-07-07 17:03:47
接受的答案解釋了爲什麼發生這種情況,我的答案使它正常工作。 – Anton 2009-08-04 20:27:52