Yesterday a guy in the group ran into this:
After I use Array.push(Object), why does the array end up holding only the contents of the last N item? For example: I want to push 1 Computer News official website—China’s computer enthusiast interactive portal, 2 virtual hosting, enterprise mail, server rental, 3 If you cannot access the website normally, please contact our company. But when I push the third one, the contents in the Array are: 3 If you cannot access the website normally, please contact our company, 3 If you cannot access the website normally, please contact our company, 3 If you cannot access the website normally, please contact our company.
Solution: new an Object every time you push.
The overturned cart ahead is a warning to those behind, and this situation is a great lesson to learn from. Note: since in AS3 everything except the primitive data types (Boolean, int, Number, String, uint) is a reference type — Object and Array are both reference types — after you push the Object three times in the loop, what the array holds is a reference to that Object. Assigning contents to the Object three times actually changes the contents at that Object’s memory address. So by the time the loop finishes assigning, the Object still points to the contents at the same memory address, and its final contents are “If you cannot access the website normally, please contact our company.” Therefore, we need three Objects pointing to different memory addresses to push, which means the Object pushed each time has to be created with its own separate new.
Reference: Flash ActionScript 3.0: The Road to Mastery, Chapter 2
A note I made back then: http://www.flashj.cn/pjblog/article.asp?id=194