【是数字但不是有效数字问题】 var quantity:Number = 15 - "rabbits"; trace(typeof quantity); //显示: "number" , 但它是NaN (not a number) trace(quantity is Number); //true trace(quantity != NaN); //false //使用isNaN()函数检测: isNaN(quantity); //true //检测变量是否内含有效数字: !isNaN(quantity); //false
【取消默认的严格编译模式】 在"Navigator"窗框里右键你的项目>选中"Properties">选中"ActionScript Compiler">取消"Enabel compile-time type checking"选项
【基元数据类型和复杂数据类型好比"值类型"和"引用类型"】 基元数据类型类似按值传递: var intOne:int = 1; var intTwo:int = 1; trace(intOne == intTwo); //true
【复杂数据类型类似按引用传递】 var arrayOne:Array = new Array("a", "b"); var arrayTwo:Array = arrayOne; trace(arrayOne == arrayTwo); //true //----------------------------------- var arrayOne:Array = new Array("a", "b"); var arrayTwo:Array = new Array("a", "b"); trace(arrayOne == arrayTwo); //false
【隐式的取出方法(getter)和设定方法(setter)】 public function get count():uint { return _count; } public function set count(value:uint):uint { if(value < 100){ _count = value; }else { throw Error(); } }
【检测播放器类型】 flash.system.Capabilities.playerType 可能的值有: "StandAlone",用于独立的 Flash Player "External",用于外部的 Flash Player 或处于测试模式下 "PlugIn",用于 Flash Player 浏览器插件 "ActiveX",用于 Microsoft Internet Explorer 使用的 Flash Player ActiveX 控件
【删除数组中第一个元素并返回该元素,使用shift()方法】 var letters:Array = new Array("a", "b", "c"); var firstLetter:String = letters.shift(); trace(letters); //输出: b,c trace(firstLetter); //输出: a
【删除数组中最后一个元素并返回该元素的值,使用pop()方法】 var letters:Array = new Array("a", "b", "c"); trace(letters); //输出: a,b,c var letter:String = letters.pop(); trace(letters); //输出: a,b trace(letter); //输出: c
【查找数组中第一个相匹配的元素】 var array:Array = ["a", "b", "c", "d", "a", "b", "c", "d"]; var match:String = "b"; for(var i:int = 0; i < array.length; i++) { if(array[i] == match) { trace("Element with index " + i + " found to match " + match); //输出: Element with index 1 found to match b break; } }
【查找数组中最后一个相匹配的元素】 var array:Array = ["a", "b", "c", "d", "a", "b", "c", "d"]; var match:String = "b"; for(var i:int = array.length - 1; i >= 0; i--) { if(array[i] == match) { trace("Element with index " + i + " found to match " + match); //输出: Element with index 5 found to match b break; } }
【把字符串转成数组】 使用String.split()方法: var list:String = "I am YoungBoy."; var words:Array = list.split(" "); //以空格作为分隔符将字符串切割 trace(words); //输出: I,am,YoungBoy.
【把数组转成字符串】 使用String.join()方法: var myArr:Array = new Array("one", "two", "three"); var myStr:String = myArr.join(" and "); trace(myArr); //输出: one,two,three trace(myStr); //输出: one and two and three
【使用对象数组处理相关数据】 var cars:Array = new Array(); cars.push({make:"Mike", year:1997, color:"blue"}); cars.push({make:"Kelly", year:1986, color:"red"}); for(var i:int = 0; i < cars.length; i++) { trace(cars[i].make + " - " + cars[i].year + " - " + cars[i].color); } //输出: // Mike - 1997 - blue // Kelly - 1986 - red