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

  • Home
  • SEARCH
  • 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 746695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:07:12+00:00 2026-05-14T14:07:12+00:00

For instance this code: function stuff() { this.onlyMethod = function () { return something;

  • 0

For instance this code:

function stuff() {
  this.onlyMethod = function () {
    return something;
  }
}

// some error is thrown
stuff().nonExistant();

Is there a way to do something like PHP’s __call as a fallback from inside the object?

function stuff() {
  this.onlyMethod = function () {
    return something;
  }
  // "catcher" function
  this.__call__ = function (name, params) {
    alert(name + " can't be called.");
  }
}

// would then raise the alert "nonExistant can't be called".
stuff().nonExistant();

Maybe I’ll explain a bit more what I’m doing.

The object contains another object, which has methods that should be accessible directly through this object. But those methods are different for each object, so I can’t just route them, i need to be able to call them dynamically.

I know I could just make the object inside it a property of the main object stuff.obj.existant(), but I’m just wondering if I could avoid it, since the main object is sort of a wrapper that just adds some functionality temporarily (and makes it easier to access the object at the same time).

  • 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-14T14:07:13+00:00Added an answer on May 14, 2026 at 2:07 pm

    There is a way to define a generic handler for calls on non-existant methods, but it is non-standard. Checkout the noSuchMethod for Firefox. Will let you route calls to undefined methods dynamically. Seems like v8 is also getting support for it.

    To use it, define this method on any object:

    var a = {};
    
    a.__noSuchMethod__ = function(name, args) {
        console.log("method %s does not exist", name);
    };
    
    a.doSomething(); // logs "method doSomething does not exist"
    

    However, if you want a cross-browser method, then simple try-catch blocks if the way to go:

    try {
        a.doSomething();
    }
    catch(e) {
        // do something
    }
    

    If you don’t want to write try-catch throughout the code, then you could add a wrapper to the main object through which all function calls are routed.

    function main() {
        this.call = function(name, args) {
            if(this[name] && typeof this[name] == 'function') {
                this[name].call(args);
            }
            else {
                // handle non-existant method
            }
        },
        this.a = function() {
            alert("a");
        }
    }
    
    var object = new main();
    object.call('a') // alerts "a"
    object.call('garbage') // goes into error-handling code
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in a function that goes something like this: void foo()
My understanding is nothing will happen. For instance this code: foreach (var some in
I wrote something like this: instance Functor (Either e) where fmap _ (Left a)
UPDATED: Added some sample code to help clarify. Hi, Feel like this shouldn't be
How to get generic interface type for an instance ? Suppose this code: interface
This is my svg code for instance. <rect x=1531.718 y=1688.217 opacity=0.8 fill=#FEE880 enable-background=new width=40.67
I perform this kind of query in my Python code to conn = rdbms.connect(instance=_INSTANCE_NAME,
If I create a SslStream instance like this: secureStream = new SslStream(stream, true, tlsRemoteCallback,
Background: I recently completed a tic-tac-toe game in AS3, using some simple function-based code
I have a function that runs some fairly generic code that does a lot

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.