Monday, May 19th, 2008

dojox.lang.aspect: More than just interception

Category: Dojo, JavaScript, Library

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:


  1. var fib = new Fibonacci;
  3. // we will time the calculations
  4. aop.advise(fib, "calculate", aop.timer("fib");
  6. fib.calculate(15);
  7. fib.setOrder(0);
  8. fib.calculate(15);
  10. // now lets use memoization
  11. aop.advise(fib, "calculate", aop.memoizer());
  12. aop.advise(fib, /^set/, aop.memoizerGuard("calculate"));
  14. fib.setOrder(1); // set order back to 1 - regular Fibonacci numbers
  15. fib.calculate(15);
  16. 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.

Posted by Dion Almaer at 7:53 am

3.5 rating from 33 votes


Comments feed TrackBack URI

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!

Comment by ilazarte — May 19, 2008

I like how it removes the overhead if you later unadvise.

Comment by JustinMeyer — May 20, 2008

There’s also an AOP plugin for jQuery:

Comment by gnz — May 20, 2008

wow, what a gem. I love JQuery. Thanks gnz.

Comment by ilazarte — May 20, 2008

Leave a comment

You must be logged in to post a comment.