I love AS3. However after some time working with Javascript, I’ve come to miss the versatility of AS2 dynamic typing. Don’t get me wrong, AS3 strong typing is great, and the compile time errors save tons of debugging hours. But sometimes strong typing can lead to multiple functions that do the same thing, just because one argument type is different.
Some languages, like Java, support multiple method signatures. AS3 doesn’t, but the guys at Adobe do something similar with methods like String.replace, where the last argument can be either a String or a Function. They do it giving the second argument an Object type. Yes, the core Object class. Everybody knows how this class works, but with all the cool features of AS3 we tend to forget it. For example, to emulate the String.replace behavior we can do something like this.
function replace(pattern:*, repl:Object):String{
if(repl is String)
// do something when repl is a String
else if(repl is Function)
// do something when repl is a Function
else
throw(new Error("The second argument of String has to be either a String or a Function"));
}
I don’t remember where I read that using is to test type casting is faster than doing something like…
String(my_string) != null
… is slower than is, I don’t know if it’s true but I do it. Altough is sometimes behaves oddly.
For parameters or options we can also use hasOwnProperty to test if a parameter has been set like this:
function modify_image(img:*, params:Object = null):void{
if(!params) params = {width: 99}; //set default values if no parameters given
else { // verify if the params object has all the necessary parameters for the method to work
if(!params.hasOwnProperty("width")) params.width = 99;
}
// do something
}
Also lets not forget AS’ Javascript inheritance. We can use method closures and the …(rest) parameter (similar to Javascript’s argument variable).
I personally use these kind of techniques with helper or configuration classes. These kind of classes focus on one task and so they are easier to test; unlike interactive components, where more robust code is preferable.
Making AS3 more dynamic with Objects
I love AS3. However after some time working with Javascript, I’ve come to miss the versatility of AS2 dynamic typing. Don’t get me wrong, AS3 strong typing is great, and the compile time errors save tons of debugging hours. But sometimes strong typing can lead to multiple functions that do the same thing, just because one argument type is different.
Some languages, like Java, support multiple method signatures. AS3 doesn’t, but the guys at Adobe do something similar with methods like String.replace, where the last argument can be either a String or a Function. They do it giving the second argument an Object type. Yes, the core Object class. Everybody knows how this class works, but with all the cool features of AS3 we tend to forget it. For example, to emulate the String.replace behavior we can do something like this.
function replace(pattern:*, repl:Object):String{ if(repl is String) // do something when repl is a String else if(repl is Function) // do something when repl is a Function else throw(new Error("The second argument of String has to be either a String or a Function")); }I don’t remember where I read that using is to test type casting is faster than doing something like…
… is slower than is, I don’t know if it’s true but I do it. Altough is sometimes behaves oddly.
For parameters or options we can also use hasOwnProperty to test if a parameter has been set like this:
function modify_image(img:*, params:Object = null):void{ if(!params) params = {width: 99}; //set default values if no parameters given else { // verify if the params object has all the necessary parameters for the method to work if(!params.hasOwnProperty("width")) params.width = 99; } // do something }Also lets not forget AS’ Javascript inheritance. We can use method closures and the …(rest) parameter (similar to Javascript’s argument variable).
I personally use these kind of techniques with helper or configuration classes. These kind of classes focus on one task and so they are easier to test; unlike interactive components, where more robust code is preferable.