Download - Stuff you didn't know about action script
![Page 1: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/1.jpg)
Stuff You Didn't Know About ActionScript
Christophe Herreman@herrodius
![Page 2: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/2.jpg)
... 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 counter-intuitive 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 ...
![Page 3: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/3.jpg)
About meRun 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
![Page 4: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/4.jpg)
Operators
![Page 5: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/5.jpg)
Checks if 2 values are equal, and applies data conversion if the values have different data types. "hello" == "hello" // true"5" == 5 // truetrue == 1 // truefalse == 0 // true"true" == true // falsenull == undefined // true
Equality "=="
![Page 6: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/6.jpg)
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
Strict Equality "==="
![Page 7: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/7.jpg)
Complex data types are compared by reference, not value. var a:Array = [1, 2, 3];var b:Array = [1, 2, 3];
a == b // false
var c:Array = a;
a == c // true
Equality "=="
![Page 8: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/8.jpg)
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;}
Conditional "?:"
![Page 9: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/9.jpg)
function (a:Object) {a ||= new Object();
} // shorthand for function (a:Object) {
if (a === null) {a = new Object();
}}
Logical OR "||="
![Page 10: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/10.jpg)
function toHTMLTag (s:String) {s &&= "<" + s + ">";return s;
} // shorthand for function toHTMLTag (s:String) {
if (s !== null && (s.length > 0))s = "<" + s + ">";
return s;}
Logical AND "&&="
![Page 11: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/11.jpg)
Casts a value to another data type, returning null if the cast fails. "hello" as String // "hello"5 as String // nulltrue as MyClass // null String("hello") // "hello"String(5) // "5"MyClass(true) // Runtime Error
"as"
![Page 12: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/12.jpg)
Check if a value is of a certain data type. var s:Sprite = new Sprite(); s is Sprite // trues is DisplayObject // trues is IEventDispatcher // true s instanceof Sprite // trues instanceof DisplayObject // trues instanceof IEventDispatcher // false
"is" vs "instanceof"
![Page 13: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/13.jpg)
Identifies the namespace of an object. public namespace Dutch;public namespace French; Dutch function hello():String {
return "hallo";} French function hello():String {
return "bonjour";}
"::" name qualifier
![Page 14: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/14.jpg)
Dutch::hello() // "hallo"French::hello() // "bonjour"
"::" name qualifier
![Page 15: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/15.jpg)
"public", "private", "protected", "internal" are also namespaces. public function get a():String;private function set a(value:String); trace(a) // compile errora = "hello" // compile error trace(public::a)private::a = "hello"
"::" name qualifier
![Page 16: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/16.jpg)
Check if an object contains a certain property. "CASEINSENSITIVE" in Array // true"CASEINSENSITIVE" in [] // false"length" in Array // true"length" in [] // true [].hasOwnProperty("CASEINSENSITIVE") // false[].hasOwnProperty("length") // true
"in" vs Object.hasOwnProperty
![Page 17: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/17.jpg)
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
"arguments"
![Page 18: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/18.jpg)
Pass 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
"..." Rest Arguments
![Page 19: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/19.jpg)
Tips & Tricks
![Page 20: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/20.jpg)
var a:Array = new Array();var a:Array = []; // faster var o:Object = new Object();var o:Object = {}; // faster var v:Vector.<String> = new Vector.<String>();v.push("a");v.push("b"); var v:Vector.<String> = new <String>["a", "b"];
Object creation
![Page 21: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/21.jpg)
var a:Object = {};a.name = "John"; var b:Object = a;b.name = "Elvis"; trace(a.name); // output "Elvis"
Object References
![Page 22: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/22.jpg)
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();
}
Object Copies
![Page 23: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/23.jpg)
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);}
}
Events
![Page 24: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/24.jpg)
var 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
for...in vs. for each...in
![Page 25: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/25.jpg)
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
trace()
![Page 26: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/26.jpg)
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;}
}}
Labeled Loops
![Page 27: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/27.jpg)
Declare a single function in an *.as file and name the file the same as the function. // in file: myGlobalFunction.aspackage {
function myGlobalFunction():void {trace("in myGlobalFunction");
}}
Global Functions
![Page 28: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/28.jpg)
Extend the behavior of built-in classes by adding methods to the 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
Adding Methods to Built-in Classes
![Page 29: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/29.jpg)
Gotchas&
WTF's
![Page 30: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/30.jpg)
Castingvar 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!
![Page 31: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/31.jpg)
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
![Page 32: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/32.jpg)
Array classvar a:Array = new Array(); // empty arrayvar a:Array = []; // empty array var a:Array = new Array(10); // array with length 10var a:Array = [10]; // array with 1 element: 10 var a:Array = new Array(1, 2, 3); // array with values 1, 2, 3var a:Array = [1, 2, 3]; // array with values 1, 2, 3
![Page 33: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/33.jpg)
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:00 public function Date(
yearOrTimevalue:Object,month:Number, // 0 to 11date:Number = 1, // 1 to 31hour:Number = 0, // 0 to 23minute:Number = 0, // 0 to 59second:Number = 0, // 0 to 59millisecond:Number = 0) // 0 to 999
![Page 34: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/34.jpg)
Throwing PartiesYou can throw more than just errors. class AwesomeParty {} try {
throw new AwesomeParty();} catch (party:AwesomeParty) {
// go loose at moNo!} Practical use?
![Page 35: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/35.jpg)
More info
ActionScript 3 Language Referencehttp://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/
ActionScript 3 Language Specificationhttp://livedocs.adobe.com/specs/actionscript/3/
Twitter@herrodius, @stackandheap Stack & Heap Labshttp://labs.stackandheap.com
![Page 36: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/36.jpg)
Questions ?
![Page 37: Stuff you didn't know about action script](https://reader034.vdocuments.site/reader034/viewer/2022042501/5562dd3ed8b42a6c498b5795/html5/thumbnails/37.jpg)
Thank you !