2012年4月5日 星期四
Actionscript3 and JavaScript 的 for . in
測試環境:
OS:windows7
IDE:flashBuider 4.6
Flex SDK: 4.6.0
ActionScript3的for ... in
特性:
範例程式碼:
//建立一個陣列,內含三個元素
private var array:Array = ["one" , "two" , "three"];
//建立一個物件,內有三個屬性name , age , gender.
private var object:Object = {name:"小米" , age:"20" , gender:"男"};
protected function applicationCompleteHandler(event:FlexEvent):void
{
testArray();
trace("----------------------------");
testObject();
}
protected function testArray():void
{
//注意這裡取回的值會是字串型態
for (var index:String in array)
{
trace(index);
}
}
protected function testObject():void
{
for(var key:String in object)
{
trace(key);
}
}
OS:windows7
IDE:flashBuider 4.6
Flex SDK: 4.6.0
ActionScript3的for ... in
特性:
- 對Array會取回index。
- 對Object取回key(即屬性名稱)。
範例程式碼:
//建立一個陣列,內含三個元素
private var array:Array = ["one" , "two" , "three"];
//建立一個物件,內有三個屬性name , age , gender.
private var object:Object = {name:"小米" , age:"20" , gender:"男"};
protected function applicationCompleteHandler(event:FlexEvent):void
{
testArray();
trace("----------------------------");
testObject();
}
protected function testArray():void
{
//注意這裡取回的值會是字串型態
for (var index:String in array)
{
trace(index);
}
}
protected function testObject():void
{
for(var key:String in object)
{
trace(key);
}
}
JavaScript Array length屬性的特性
javascript code:
var _myArray = new Array(5);
alert( _myArray.length);
_myArray[100] = 50;
alert(_myArray.length);
alert(_myArray.length);
Array一開始宣告長度為5
而執行完_myArray[100] = 50後,長度會變成101。
試了一下ActionScript3同JavaScript。
而在Java這種比較嚴謹的語言則會發生
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at ut.com.LdapTools.main(LdapTools.java:21)
Java Code:
int[] _myArray = new int[5];
System.out.println(_myArray.length);
_myArray[100] = 50;
System.out.println(_myArray.length);