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 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

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Basically yes, however, in JavaScript functions are first class objects,… May 14, 2026 at 9:14 pm
  • Editorial Team
    Editorial Team added an answer When you create the Process instance for tracert, you need… May 14, 2026 at 9:14 pm
  • Editorial Team
    Editorial Team added an answer In my experience, the add to source control mechanism tries… May 14, 2026 at 9:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.