Max Blog

My Blog

Parenthesis – the round mystery

Parenthesis has a really interesting field of application in AS3.

Have a look at this page: Parentheses

The interesting one is to evaluate a series of expressions:

?View Code ACTIONSCRIPT
var a:int = 2;
var b:int = 3;
trace((a++, b++, a+b)); // 7

Very cool stuff it looks like an inlined function. Normally I would write something less creative like this

?View Code ACTIONSCRIPT
function foo(a:int, b:int):int{
    a++;
    b++;
    return a+b; 
}
trace(foo(2,3)); // 7

I also would say that second version is more readable and reusable, but not as cool as the first one.

Another interesting thing about this notation is that you can’t declare variables inside parenthesis, but you can declare anonymous functions:

?View Code ACTIONSCRIPT
var f:Function;
trace((f = function():int{return 7;}, f())); // 7

And that’s not all folks. There is another context where parenthesis have a pretty cool role  - E4X. In E4X you use parenthesis for “filters” declaration.

There are a few nice posts about E4X filters on this blog: http://joshblog.net/2007/05/08/methods-to-filter-data-with-e4x-in-flash-9/

posted by admin in AS3 Creative point of view and have No Comments

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment
Before you submit form:
Human test by Not Captcha