Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1076959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:30:20+00:00 2026-05-16T21:30:20+00:00

There is a noSuchMethod feature in some javascript implementations (Rhino, SpiderMonkey) proxy = {

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:30:20+00:00Added an answer on May 16, 2026 at 9:30 pm

    UPDATE: ECMAScript 6 Proxies are widely supported now. Basically, if you don’t need to support IE11, you can use them.

    Proxy objects allow you to define custom behavior for fundamental operations, like property lookup, assignment, enumeration, function invocation, etc.

    Emulating __noSuchMethod__ with ES6 Proxies

    By implementing traps on property access, you can emulate the behavior of the non-standard __noSuchMethod__ trap:

    function enableNoSuchMethod(obj) {
      return new Proxy(obj, {
        get(target, p) {
          if (p in target) {
            return target[p];
          } else if (typeof target.__noSuchMethod__ == "function") {
            return function(...args) {
              return target.__noSuchMethod__.call(target, p, args);
            };
          }
        }
      });
    }
    
    // Example usage:
    
    function Dummy() {
      this.ownProp1 = "value1";
      return enableNoSuchMethod(this);
    }
    
    Dummy.prototype.test = function() {
      console.log("Test called");
    };
    
    Dummy.prototype.__noSuchMethod__ = function(name, args) {
      console.log(`No such method ${name} called with ${args}`);
      return;
    };
    
    var instance = new Dummy();
    console.log(instance.ownProp1);
    instance.test();
    instance.someName(1, 2);
    instance.xyz(3, 4);
    instance.doesNotExist("a", "b");

    Original 2010 answer

    There is only one existing thing at the moment that can actually do what you want, but unfortunately is not widely implemented:

    • ECMAScript Harmony Proxies.

    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(p.world); // alerts 'Hello, world'
    p.foo = 'bar';  // alerts foo=bar
    

    Try it out here.

    EDIT: The proxy API evolved, the Proxy.create method was removed in favor of using the Proxy constructor, see the above code updated to ES6:

    const obj = {};
    const p = new Proxy(obj, {
      get(target, prop) {        // intercepts property access
        return 'Hello, '+ prop;
      },
      set(target, prop, value, receiver) { // intercepts property assignments
        console.log(prop +'='+ value);
        Reflect.set(target, prop, value, receiver)
        return true;
      }
    });
    
    console.log(p.world);
    p.foo = 'bar';

    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 proxy.name
    enumerate():                              // for (name in proxy)
    fix():                                    // Object.{freeze|seal|preventExtensions}(proxy)
    
    // Derived traps
    has(name):                                // name in proxy
    hasOwn(name):                             // ({}).hasOwnProperty.call(proxy, name)
    get(receiver, name):                      // receiver.name
    set(receiver, name, val):                 // receiver.name = val
    keys():                                   // Object.keys(proxy)
    

    The only resource I’ve seen, besides the proposal by itself, is the following tutorial:

    • Harmony Proxies: Tutorial

    Edit: More information is coming out, Brendan Eich recently gave a talk at the JSConf.eu Conference, you can find his slides here:

    • Proxies are Awesome!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is an interesting feature in the Foursquare App's TabBar The central button in
There are some stdlib functions that throw errors on invalid input. For example: Prelude>
There is some contradiction in the api documentation: on one location: https://developer.foursquare.com/docs/responses/user on another
There was some progress made to the earlier problem. Now there is a new
There is some old legacy vbscript code we have that needs some sort of
There are some incompatible changes in code which I depend on. So I want
There are two popular closure styles in javascript. The first I call anonymous constructor
There are a few ways to get class-like behavior in javascript, the most common
There are so many different ways to include JavaScript in a html page. I
There are many questions about this PersistenceException, but I have not seen some, where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.