Loop Through Object in Actionscript3.0
2011-11-01 14:52
405 查看
In Actionscript3.0, there is a method to iterate through an object:
will output:
Have no idea about why the order is 3-1-4-2!?, whatever, please note
the differences between 'for..in' and 'for each..in'.
This works because the variable '$obj' is weak-typed, and 'Object' should be dynamic class. It will not work on strong
typed object. Let's try that. Create a class file: StrongObject.as
And add the lines to .fla:
That will output nothing!!!
var $obj:Object = {}; $obj.prop1 = "value-1"; $obj.prop2 = "value-2"; $obj.prop3 = "value-3"; $obj.prop4 = "value-4"; $obj["prop5"] = "value-5"; var $propname:String = "prop6"; $obj[$propname] = "value-6"; for (var $prop:String in $obj) { trace($prop + ": " + $obj[$prop]); } for (var $item in $obj) { trace($item); } for each (var $itemm in $obj) { trace($itemm); }
will output:
prop3: value-3 prop6: value-6 prop1: value-1 prop4: value-4 prop5: value-5 prop2: value-2 prop3 prop6 prop1 prop4 prop5 prop2 value-3 value-6 value-1 value-4 value-5 value-2
Have no idea about why the order is 3-1-4-2!?, whatever, please note
the differences between 'for..in' and 'for each..in'.
This works because the variable '$obj' is weak-typed, and 'Object' should be dynamic class. It will not work on strong
typed object. Let's try that. Create a class file: StrongObject.as
package { public class StrongObject { public var prop1:String; public var prop2:int; public var prop3:Boolean; public var prop4:Number; public function StrongObject( $val1:String, $val2:int, $val3:Boolean, $val4:Number ) { this.prop1 = $val1; this.prop2 = $val2; this.prop3 = $val3; this.prop4 = $val4; } } }
And add the lines to .fla:
var $sobj:StrongObject = new StrongObject("test", 33, false, 45.6); for(var $sprop:String in $sobj) { trace($sprop + ": " + $sobj[$sprop]); }
That will output nothing!!!
相关文章推荐
- Detect Chinese character through out a string in Actionscript3.0
- Looking up through Array in Actionscript 3.0
- Uncaught Error: Error calling method on NPObject 或者uncaught exception: Error in Actionscript. Use a
- Alternatives to Array in Actionscript3.0
- Reflection in Actionscript 3.0/Flex 2
- Object-Oriented ActionScript 3.0
- root, stage and timeline in ActionScript3.0
- Capture Keyboard Event in Actionscript3.0
- Bitmap & BitmapData classes in Actionscript 3.0
- Constructor Overloading in Actionscript 3.0
- Custom Event in ActionScript3.0
- Create a Drag and Drop Puzzle in ActionScript 3.0
- Empty, Unavailable, Invalid Variables Handling in Actionscript 3.0 & PHP 5+
- Garbage Collection in Actionscript3.0
- Flex ActionScript 3.0 SharedObject 本地共享对象 操作
- NativeScript官方书籍:NativeScript in Action-用你现有技术构建移动应用程序
- The Essential Guide to Flex 2 with ActionScript 3.0
- How to Fake a AsyncToken return in ActionScript 3
- ASP.NET AJAX in Action—ScriptManager
- flex ActionScript 库 objectHandelrs实现图片旋转,拖拽操作