Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Stuff you didn't know about action script

17,465 views

Published on

Published in: Technology

Stuff you didn't know about action script

  1. Stuff You Didnt Know About ActionScript Christophe Herreman @herrodius
  2. ... or an exploration of the ActionScript 3 language in whichyou might discover new things that you were not aware ofand that one day might come in handy, or in which you willwant to scream out WTFs so loud that the whole venuemight think Im presenting naked whilst in reality you are justlooking at a piece of ActionScript code that is so counter-intuitive or looks rather puzzling you wonder how anyonecame up with the idea to implement certain parts of the APIthis way, which on the other hand makes the language alsovery fun to work with once you know how to deal with thesepeculiarities and it becomes clear that ActionScript isactually pretty cool and powerful ...
  3. About meRun Stack & Heap, a development, consulting and trainingcompany based in Belgium specializing in Web / RIADevelopmentCertified expert Flex with AIR, loved Flash since 1999Founder of Spring ActionScript and AS3CommonsApache Flex committer
  4. Operators
  5. Equality "=="Checks if 2 values are equal, and applies data conversion ifthe values have different data types."hello" == "hello" // true"5" == 5 // truetrue == 1 // truefalse == 0 // true"true" == true // falsenull == undefined // true
  6. Strict Equality "==="Checks if 2 values and their types are equal."hello" === "hello" // true"5" === 5 // compile errortrue === 1 // falsefalse === 0 // false"true" === true // falsenull === undefined // false
  7. Equality "=="Complex data types are compared by reference, not value.var a:Array = [1, 2, 3];var b:Array = [1, 2, 3];a == b // falsevar c:Array = a;a == c // true
  8. Conditional "?:"Known as the Ternary Operator.var result:Boolean = (a > b) ? x : y;// shorthand forvar result:Boolean;if (a > b) { result = x;} else { result = y;}
  9. Logical OR "||="function (a:Object) { a ||= new Object();}// shorthand forfunction (a:Object) { if (a === null) { a = new Object(); }}
  10. Logical AND "&&="function toHTMLTag (s:String) { s &&= "<" + s + ">"; return s;}// shorthand forfunction toHTMLTag (s:String) { if (s !== null && (s.length > 0)) s = "<" + s + ">"; return s;}
  11. "as"Casts a value to another data type, returning null if the castfails."hello" as String // "hello"5 as String // nulltrue as MyClass // nullString("hello") // "hello"String(5) // "5"MyClass(true) // Runtime Error
  12. "is" vs "instanceof"Check if a value is of a certain data type.var s:Sprite = new Sprite();s is Sprite // trues is DisplayObject // trues is IEventDispatcher // trues instanceof Sprite // trues instanceof DisplayObject // trues instanceof IEventDispatcher // false
  13. "::" name qualifierIdentifies the namespace of an object.public namespace Dutch;public namespace French;Dutch function hello():String { return "hallo";}French function hello():String { return "bonjour";}
  14. "::" name qualifierDutch::hello() // "hallo"French::hello() // "bonjour"
  15. "::" name qualifier"public", "private", "protected", "internal"are also namespaces.public function get a():String;private function set a(value:String);trace(a) // compile errora = "hello" // compile errortrace(public::a)private::a = "hello"
  16. "in" vs Object.hasOwnPropertyCheck if an object contains a certainproperty."CASEINSENSITIVE" in Array // true"CASEINSENSITIVE" in [] // false"length" in Array // true"length" in [] // true[].hasOwnProperty("CASEINSENSITIVE") // false[].hasOwnProperty("length") // true
  17. "arguments"An array available in each function that contains thearguments 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
  18. "..." Rest ArgumentsPass an arbitrary number of extra arguments to a function.function myFunction (x:int, ... rest) { for (var i:uint = 0; i< rest.length; i++) { trace(rest[i]); }}myFunction(1, 2, 3);// 2// 3
  19. Tips & Tricks
  20. Object creationvar a:Array = new Array();var a:Array = []; // fastervar o:Object = new Object();var o:Object = {}; // fastervar v:Vector.<String> = new Vector.<String>();v.push("a");v.push("b");var v:Vector.<String> = new <String>["a", "b"];
  21. Object Referencesvar a:Object = {};a.name = "John";var b:Object = a;b.name = "Elvis";trace(a.name); // output "Elvis"
  22. Object CopiesCreate deep or shallow copies depending onthe scenario.// deep copyprivate function clone(obj:Object):Object { var bytes:ByteArray = new ByteArray(); bytes.writeObject(obj); bytes.position = 0; return bytes.readObject();}
  23. EventsAlways 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); }}
  24. for...in vs. for each...invar arr:Array = ["a", "b", "c"];// loops through keys (0, 1, 2)for ( var i in arr ) { trace( i );}// loop through values ("a", "b", "c")for each ( var s:String in arr ) { trace( s );}Gotcha: order is not guaranteed, use for loop with counter
  25. trace()You can pass multiple arguments to the trace() method. Noneed 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.141592653589793true
  26. Labeled LoopsUse "label" on a loop to name it. Useful when breaking fromnested 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; } }}
  27. Global FunctionsDeclare a single function in an *.as file and name the file thesame as the function.// in file: myGlobalFunction.aspackage { function myGlobalFunction():void { trace("in myGlobalFunction"); }}
  28. Adding Methods to Built-in ClassesExtend the behavior of built-in classes by adding methods tothe prototype.Array.prototype.removeItem = function (item:*):void { var index:int = this.indexOf(item); if (index > -1) { this.splice(index, 1); }};var a:Array = [1, 2, 3];a.removeItem(2);trace(a); // 1, 3
  29. Gotchas & WTFs
  30. Castingvar o:MyObject = new MyObject();var o1:MyObject = MyObject(o);var o2:MyObject = o as MyObject; // o1 === o2var a:Array = [1, 2, 3];var a1:Array = Array(a); // new Array !!!var a2:Array = a as Array; // a1 !== a2Also the case with Date and Error classes. Watch out!
  31. Casting to BooleanBoolean(true) // trueBoolean(false) // falseBoolean(0) // falseBoolean(1) // trueBoolean(-1) // trueBoolean("true") // trueBoolean("false") // trueBoolean("") // falseBoolean(" ") // trueBoolean("0") // trueBoolean("1") // trueBoolean(null) // falseBoolean(undefined) // falseBoolean(Object) // trueBoolean({}) // true
  32. Array classvar a:Array = new Array(); // empty arrayvar a:Array = []; // empty arrayvar a:Array = new Array(10); // array with length 10var a:Array = [10]; // array with 1 element: 10var a:Array = new Array(1, 2, 3); // array with values 1, 2,3var a:Array = [1, 2, 3]; // array with values 1, 2,3
  33. Date classnew Date(); // current datenew Date(2012); // 01/01/1970 01:00:02new Date(2012, 1); // 01/02/2012 00:00:00new Date(2012, 1, 1); // 01/02/2012 00:00:00public function Date( yearOrTimevalue:Object, month:Number, // 0 to 11 date:Number = 1, // 1 to 31 hour:Number = 0, // 0 to 23 minute:Number = 0, // 0 to 59 second:Number = 0, // 0 to 59 millisecond:Number = 0) // 0 to 999
  34. Throwing PartiesYou can throw more than just errors.class AwesomeParty {}try { throw new AwesomeParty();} catch (party:AwesomeParty) { // go loose at moNo!}Practical use?
  35. More infoActionScript 3 Language Referencehttp://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/ActionScript 3 Language Specificationhttp://livedocs.adobe.com/specs/actionscript/3/Twitter@herrodius, @stackandheapStack & Heap Labshttp://labs.stackandheap.com
  36. Questions ?
  37. Thank you !

×