Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic proxies in javascript?

Tags:

I can proxy a single function in javascript by doing something like this (just jotted down from memory so bear with me)

function addAroundAdvice(target){
    var targetFunction = target.aFunction;
    target.aFunction = new function(){
           invokePreCall();
           targetFunction.apply(target, arguments);
           invokePostCall();
    }
}

Being a java programmer I'd think of this as a dynamic proxy. Every time I write code like this I think that someone must have made a really smart library that does the common proxying operations that is at least 10% better than what I can do in a hurry. I'd be expecting some stuff like correctly intercepting all methods for any given object, which may not be entirely trivial. Then there's different types of advice. So while I'm not expecting something the size of scriptaculous, it's certainly more than 6 lines of code.

So where are these libraries ?

like image 216
krosenvold Avatar asked Feb 20 '09 07:02

krosenvold


2 Answers

Try jQuery AOP plugin

Looking at the source it seems that only uses jQuery as a namespace, so you could try this plugin even if don't want to use jQuery.

like image 81
Serxipc Avatar answered Oct 12 '22 03:10

Serxipc


The Dojo Toolkit has a lot of support for AOP constructs like this:
Eugene Lazutkin's Blog Post on Aspect Oriented Programming with Dojo

like image 43
Gavin Avatar answered Oct 12 '22 02:10

Gavin