Monday, May 19th, 2008
dojox.lang.aspect: More than just interception
Eugene Lazutkin has written a very thorough post on dojox.lang.aspect a module that takes AOP seriously in JavaScript.
As someone who has been to a couple AOSDs (the AOP software conference) and was excited to see AOP on the scene, it is good to see someone who gets it working on the JavaScript side.
Of course, we all get the interception piece. It is very easy to just wrap a method to do something new in a dynamic language such as JavaScript. But the power (and complexity!) of AOP lies in the world of joinpoints, pointcuts and worm holes :)
For these, a dynamic language isn't as helpful. Eugene delves into the world and takes us to the module from first principles. He then builds a timer aspect which can be used to profile an application. Along with a memoizer and other useful cross cutting concerns, you end up with good ole Fibonaci:
-
-
var fib = new Fibonacci;
-
-
// we will time the calculations
-
aop.advise(fib, "calculate", aop.timer("fib");
-
-
fib.calculate(15);
-
fib.setOrder(0);
-
fib.calculate(15);
-
-
// now lets use memoization
-
aop.advise(fib, "calculate", aop.memoizer());
-
aop.advise(fib, /^set/, aop.memoizerGuard("calculate"));
-
-
fib.setOrder(1); // set order back to 1 - regular Fibonacci numbers
-
fib.calculate(15);
-
fib.setOrder(0);
-
Memoization got results too:
On my computer with Firefox 3 the calculation of 1-order (regular) Fibonacci number of 15 (987) took ~48ms without memoization and 0-1ms after memoization. The calculation of 0-order Fibonacci number of 15 (32768) took ~1155ms without memoization and the same 0-1ms after. As you can see this technique can work wonders without much investment of time.
Just like with Java, we won't see every developer worrying about pointcuts day to day, but instead, simple usage of existing advice will become very useful indeed.












Working dynamic languages, I miss my aop… Stuff that was soooo simple to do in Java and then configure is painful to introduce later in a language like Php.
I haven’t needed aop in JS yet, but I’m hoping for all libraries to implement it in some form. Very cool stuff!
I like how it removes the overhead if you later unadvise.
There’s also an AOP plugin for jQuery: http://jquery-aop.googlecode.com
wow, what a gem. I love JQuery. Thanks gnz.