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.
Read more on the source site
No comments yet.
feel free to leave a comment
Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs are automatically generated. Off-topic or inappropriate comments will be edited or deleted. Email addresses will never be published. Keep it PG-13 people!
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
All fields marked with " * " are required.

