//这里是行内注释 var i:int; var x:int; //这是也是行内注释多行注释
/* 这是 一段 多行注释 */
var $number:Number; $number = "test" //语法错误,右值必需是一个数字,而不是字符串
var $string:String = new String("HELLO"); //更简洁的方式: var $str:String = "HELLO"; var $xml:XML = <root><node></node></root>; var $array:Array = ["item1","item2","item3"]; var $obj:Object = {x:1, y:2, z:3};
for(var i = 0; i < 10; i++ ){ trace(i); }
var myArray:Array = ["item1","item2", "item3"]; //这种形式的循环适合操作XML或者XMList对象 //这种形式的循环是按照属性名迭代的,而数组的属性名就是数组的索引 //大多数情况下都要使用String类型的属性名,针对数组的索引,直接使用int很可能会报错,而使用String则Flash Player会自动转换成数值 for(var i:String in myArray) { //查看trace输出需要下载Flash Player 的debug版本,并使用特定的浏览器(如IE进行调试) trace(myArray[i]); }
var myObj:Object = {firstName:"leo",lastName:"xu"}; for(var i:String in myObj) { trace("Property:" + i + "=" + myObj[i]); }
//如果只想得到每项的值,使用for each ... in 更方便 for each(var i:String in myObj) { trace(i); }
var $array1:Array = new Array(); var $array2:Array = new Array(3);操作
push() //添加到末尾 unshift() //添加到开头 pop() //末尾删除 shift() //开头删除 splice() //从某个位置删除,然后插入新项 $array.splice(1, 0, "leo xu");//在第二个位置添加一个 $array.splice(1, 1);//删除第二项 $array.splice(3, 2, "leo", "xu", "hahaha");//从第四个位置开始删除两项,再添加三个新项 var $array:Array = new ["leo", "xu", "hahaha"];//数组的简便初始化
//下面是数组的遍历 for(var i:int = 0; i<array.length; i++ {//使用length属性获取数组的大小 trace(array[i]);//把值输出到日志 }
var $map:Array = new Array(); $map["item1"] = "leo1"; $map["item2"] = "leo2"; $map["item3"] = "leo3"; trace($map["item1"]); trace($map["item2"]); trace($map["item3"]); for(var key:String in $map) { trace("key:" + key + " ,value:" + $map[key]); }
//如果只想得到每项的值,使用for each ... in 循环 for each (var i:String in $map) { trace("value:" + i); }注意:关联数组中值得存放时无序的,所以上述循环无法预言其输出顺序。
//另外一种输出键值对的方式就是直接使用Object对象 var $map:Object = new Object(); var $map:Object = {item1:"leo", item2:"xu", item3:"hahaha"};
//关联数组还可以使用点表示法来访问数组项: trace($map.item1); trace($map.item2); trace($map.item3);
import mx.collections.ArrayCollection;//导入ArrayCollection类 import mx.collections.*;//导入所有的类注意:导入内容只在执行了import语句的文件内可见。
<fx:Script source="xxx.as"/>
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Group> <s:layout> <s:HorizontalLayout/> </s:layout> <s:TextInput id="text1"/> <s:Button label="copy" click="text2.text = text1.text"/> <s:TextInput id="text2"/> </s:Group> </s:Application>
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:Group> <s:layout> <s:HorizontalLayout/> </s:layout> <s:TextInput id="text1"/> <s:TextInput id="text2" text="{text1.text}"/> </s:Group> </s:Application>
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Binding source="text1.text" destination="text2.text" twoWay="true"/> <s:Group> <s:layout> <s:HorizontalLayout/> </s:layout> <s:TextInput id="text1"/> <s:TextInput id="text2"/> </s:Group> </s:Application>
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ [Bindable] public var s:String = ""; ]]> </fx:Script> <s:Group> <s:layout> <s:HorizontalLayout/> </s:layout> <s:TextInput id="text1"/> <s:Button label="copy" click="s=text1.text"/> <s:TextInput id="text2" text="{s}"/> </s:Group> </s:Application>