Array.length is a read-write property. I should’ve know it.
I just thought it was a read-only property. I came to know it reading a copy of EAS3.0.
Array.length 속성이 리드-라이트 였네요. 이런걸 지금까지 모르고 있었다니…
당연히 Read-only일 거라고 생각하고 있었는데, EAS3.0을 보다가 알게 되었네요.
Array.lengthがread-write属性だったんですね。なぜ今まで知らなかったんだろう……
当然read-onlyだと思っていったのに、EAS3.0を読みながら分かった。
length property
length:uint [read-write]
Language Version : ActionScript 3.0
Player Version : Flash Player 9
A non-negative integer specifying the number of elements in the array. This property is automatically updated when new elements are added to the array. When you assign a value to an array element (for example, my_array[index] = value), if index is a number, and index+1 is greater than the length property, the length property is updated to index+1.
Note: If you assign a value to the length property that is shorter than the existing length, the array will be truncated.
quote from ActionScript 3.0 Language Reference
1
2
3
4
| var arr:Array = new Array(0, 1, 2, 3, 4, 5, 6);
trace(arr);// output 0,1,2,3,4,5,6
arr.length = 4;
trace(arr);// output 0,1,2,3 |
var arr:Array = new Array(0, 1, 2, 3, 4, 5, 6);
trace(arr);// output 0,1,2,3,4,5,6
arr.length = 4;
trace(arr);// output 0,1,2,3
If the newly assigned length is shorter than the existing value, items whose index is larger than the length will be removed from the array and the array will be shortened to the specified length. In some cases, it is more useful than Array.slice() or Array.splice() methods.
위와 같이 length에 현재 길이보다 작은 값을 입력하면, length 이후의 아이템들을 제거하고 배열이 입력받은 길이로 줄어들게 됩니다. 경우에 따라서는 Array.slice(), Array.splice() 보다 유용할 것 같네요.
若し、新しいlengthが現在のlengthより少ないと、それ以後の配列要素は取り去れて、その配列の長さは短くなります。時折Array.slice()やArray.splice()メソッドより便利そうです。