There is a noSuchMethod feature in some javascript implementations (Rhino, SpiderMonkey)
proxy = {
__noSuchMethod__: function(methodName, args){
return "The " + methodName + " method isn't implemented yet. HINT: I accept cash and beer bribes" ;
},
realMethod: function(){
return "implemented" ;
}
}
js> proxy.realMethod()
implemented
js> proxy.newIPod()
The newIPod method isn't implemented yet. HINT: I accept cash and beer bribes
js>
I was wondering, is there was a way to do something similar for properties? I'd like to write proxy classes that can dispatch on properties as well as methods.
I don't believe this type of metaprogramming is possible (yet) in javascript. Instead, try using the
__noSuchMethod__
functionality to achieve the effect with property getters. Not cross-browser as it's a Mozilla extension.Here's how to get behaviour similar to __noSuchMethod__
First of all, here's a simple object with one method:
Now create a Proxy which will catch access to properties/method and add your existing object as a first parameter.
Now try it:
Works in Chrome/Firefox/Opera. Doesn't work in IE(but already works in Edge). Also tested on mobile Chrome.
Creation of proxy can be automated and invisible i.e. if you use Factory pattern to build your objects. I did that to create workers which internal functions can be called directly from the main thread. Using workers can be now so simple thanks to this cool new feature called Proxy. The simplest worker implementation ever:
There is
__defineGetter__
,__defineSetter__
,__lookupGetter__
and__lookupSetter__
in addition to__noSuchMethod__
in SpiderMonkey.There is only one existing thing at the moment that can actually do what you want, but unfortunately is not widely implemented:
There are only two working implementations available at this time, in the latest Firefox 4 betas (it has been around since FF3.7 pre-releases) and in node-proxy for server-side JavaScript -Chrome and Safari are currently working on it-.
It is one of the early proposals for the next version of ECMAScript, it's an API that allows you to implement virtualized objects (proxies), where you can assign a variety of traps -callbacks- that are executed in different situations, you gain full control on what at this time -in ECMAScript 3/5- only host objects could do.
To build a proxy object, you have to use the
Proxy.create
method, since you are interested in theset
andget
traps, I leave you a really simple example:Try it out here.
The Proxy API is so new that isn't even documented on the Mozilla Developer Center, but as I said, a working implementation has been included since the Firefox 3.7 pre-releases.
The
Proxy
object is available in the global scope and thecreate
method can take two arguments, ahandler
object, which is simply an object that contains properties named as the traps you want to implement, and an optionalproto
argument, that makes you able to specify an object that your proxy inherits from.The traps available are:
The only resource I've seen, besides the proposal by itself is the following tutorial:
Edit: More information is coming out, Brendan Eich recently gave a talk at the JSConf.eu Conference, you can find his slides here: