Documente Academic
Documente Profesional
Documente Cultură
... or an exploration of the ActionScript 3 language in which you might discover new things that you were not aware of and that one day might come in handy, or in which you will want to scream out WTF's so loud that the whole venue might think I'm presenting naked whilst in reality you are just looking at a piece of ActionScript code that is so counterintuitive or looks rather puzzling you wonder how anyone came up with the idea to implement certain parts of the API this way, which on the other hand makes the language also very fun to work with once you know how to deal with these peculiarities and it becomes clear that ActionScript is actually pretty cool and powerful ...
About me
Run Stack & Heap, a development, consulting and training company based in Belgium specializing in Web / RIA Development Certified expert Flex with AIR, loved Flash since 1999 Founder of Spring ActionScript and AS3Commons Apache Flex committer
Operators
Equality "=="
Checks if 2 values are equal, and applies data conversion if the values have different data types. "hello" == "hello" "5" == 5 true == 1 false == 0 "true" == true null == undefined // true // true // true // true // false // true
Equality "=="
Complex data types are compared by reference, not value. var a:Array = [1, 2, 3]; var b:Array = [1, 2, 3]; a == b // false
Conditional "?:"
Known as the Ternary Operator. var result:Boolean = (a > b) ? x : y; // shorthand for var result:Boolean; if (a > b) { result = x; } else { result = y; }
Logical OR "||="
function (a:Object) { a ||= new Object(); } // shorthand for function (a:Object) { if (a === null) { a = new Object(); } }
"as"
Casts a value to another data type, returning null if the cast fails. "hello" as String 5 as String true as MyClass String("hello") String(5) MyClass(true) // "hello" // null // null // "hello" // "5" // Runtime Error
"is" vs "instanceof"
Check if a value is of a certain data type. var s:Sprite = new Sprite(); s is Sprite s is DisplayObject s is IEventDispatcher // true // true // true
"in" vs Object.hasOwnProperty
Check if an object contains a certain property. "CASEINSENSITIVE" in Array "CASEINSENSITIVE" in [] "length" in Array "length" in [] // // // // true false true true
"arguments"
An array available in each function that contains the arguments passed to the function. function myFunction (x:int) { for(var i:uint=0; i<arguments.length; i++){ trace(arguments[i]); } } myFunction(1, 2, 3); // 1 // 2 // 3
Object creation
var a:Array = new Array(); var a:Array = []; var o:Object = new Object(); var o:Object = {}; // faster
// faster
var v:Vector.<String> = new Vector.<String>(); v.push("a"); v.push("b"); var v:Vector.<String> = new <String>["a", "b"];
Object References
var a:Object = {}; a.name = "John"; var b:Object = a; b.name = "Elvis"; trace(a.name); // output "Elvis"
Object Copies
Create deep or shallow copies depending on the scenario. // deep copy private function clone(obj:Object):Object { var bytes:ByteArray = new ByteArray(); bytes.writeObject(obj); bytes.position = 0; return bytes.readObject(); }
Events
Always override the "clone" method in an Event subclass. Prevents runtime type coercion errors when redispatching. class MyEvent extends Event { public function MyEvent(data:Object){ _data = data; } override public function clone():Event { return new MyEvent(_data); } }
trace()
You can pass multiple arguments to the trace() method. No need to compose a string.
trace(new Date(2012, 4, 22), "Aloha", Math.PI, true); // Tue May 22 00:00:00 GMT+0200 2012 Aloha 3.141592653589793 true
Labeled Loops
Use "label" on a loop to name it. Useful when breaking from nested loops.
mainLoop: for (var i:uint = 0; i<10; i++) { for (var j:uint = 0; j<10; j++) { if (i == 5 && j == 7) { break mainLoop; } } }
Global Functions
Declare a single function in an *.as file and name the file the same as the function. // in file: myGlobalFunction.as package { function myGlobalFunction():void { trace("in myGlobalFunction"); } }
Casting
var o:MyObject = new MyObject(); var o1:MyObject = MyObject(o); var o2:MyObject = o as MyObject; // o1 === o2 var a:Array = [1, 2, 3]; var a1:Array = Array(a); // new Array !!! var a2:Array = a as Array; // a1 !== a2 Also the case with Date and Error classes. Watch out!
Casting to Boolean
Boolean(true) Boolean(false) Boolean(0) Boolean(1) Boolean(-1) Boolean("true") Boolean("false") Boolean("") Boolean(" ") Boolean("0") Boolean("1") Boolean(null) Boolean(undefined) Boolean(Object) Boolean({}) // true // false // false // true // true // true // true // false // true // true // true // false // false // true // true
Array class
var a:Array = new Array(); var a:Array = []; var a:Array = new Array(10); var a:Array = [10]; // empty array // empty array // array with length 10 // array with 1 element: 10
var a:Array = new Array(1, 2, 3); // array with values 1, 2, 3 var a:Array = [1, 2, 3]; // array with values 1, 2, 3
Date class
new new 02 new new Date(); Date(2012); // current date // 01/01/1970 01:00:
public function Date( yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0)
// 0 to // 1 // 0 // 0 to // 0 to // 0 to
11 to 31 to 23 59 59 999
Throwing Parties
You can throw more than just errors. class AwesomeParty {} try { throw new AwesomeParty(); } catch (party:AwesomeParty) { // go loose at moNo! } Practical use?
More info
ActionScript 3 Language Reference
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/
Questions ?
Thank you !