Organizational Research By

Surprising Reserch Topic

equivalent of the nosuchmethod feature for properties or a way

is there an equivalent of the nosuchmethod feature for properties or a way  using -'javascript'

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()
js> proxy.newIPod()
The newIPod method isn't implemented yet. HINT: I accept cash and beer bribes

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.

asked Sep 8, 2015 by rajesh
edited Sep 9, 2015 by rajesh
0 votes

Related Hot Questions

3 Answers

0 votes

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 the set and get traps, I leave you a really simple example:

var p = Proxy.create({
  get: function(proxy, name) {        // intercepts property access
    return 'Hello, '+ name;
  set: function(proxy, name, value) { // intercepts property assignments
    alert(name +'='+ value);
    return true;

alert(; // alerts 'Hello, world' = 'bar';  // alerts foo=bar

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 the create method can take two arguments, a handler object, which is simply an object that contains properties named as the traps you want to implement, and an optional proto argument, that makes you able to specify an object that your proxy inherits from.

The traps available are:

// TrapName(args)                          Triggered by
// Fundamental traps
getOwnPropertyDescriptor(name):           // Object.getOwnPropertyDescriptor(proxy, name)
getPropertyDescriptor(name):              // Object.getPropertyDescriptor(proxy, name) [currently inexistent in ES5]
defineProperty(name, propertyDescriptor): // Object.defineProperty(proxy,name,pd)
getOwnPropertyNames():                    // Object.getOwnPropertyNames(proxy) 
getPropertyNames():                       // Object.getPropertyNames(proxy) 
delete(name):                             // delete
enumerate():                              // for (name in proxy)
fix():                                    // Object.{freeze|seal|preventExtensions}(proxy)

// Derived traps
has(name):                                // name in proxy
hasOwn(name):                             // ({}), name)
get(receiver, name):                      //
set(receiver, name, val):                 // = val
keys():                                   // Object.keys(proxy)

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 Conference, you can find his slides here:

answered Sep 8, 2015 by rajesh
0 votes

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.

var proxy = {
    __noSuchMethod__: function(methodName, args) {
       if(methodName.substr(0,3)=="get") {
          var property = methodName.substr(3).toLowerCase();                             
          if (property in this) {
              return this[property];
    }, color: "red"
answered Sep 8, 2015 by rajesh
0 votes

There is __defineGetter__, __defineSetter__, __lookupGetter__ and __lookupSetter__ in addition to __noSuchMethod__ in SpiderMonkey. See here:

answered Sep 8, 2015 by rajesh