The code is fairly understandable as well. When you set up an object as “Decorated”, it adds
after() methods. You pass in the name of the function you want run before and another function. Then the magic kicks in. It takes a copy of the original method, and installs a new method in its place which calls all the before hooks, then the original method and finally any after methods.
The only minor disadvantage is that you can’t inspect or alter the return value in the after method. Otherwise, this piece of code is conceptually very similar to Perl’s Hook::LexWrap (a damned fine tool).
Yes, I’m sure that Dojo gives you all this too. But this is a nice isolated example.
Here’s dojo’s way of doing it:
I didn’t meant to trackback spam you. That was just me editing the article. I didn’t know typo would send you a trackback every time I saved. Sorry, man.
No problems—quickly cured!