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

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • 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 This is not supported by the Android SDK today, sorry. May 17, 2026 at 2:22 am
  • Editorial Team
    Editorial Team added an answer I use the XMLProperty ant task to parse and copy… May 17, 2026 at 2:22 am
  • Editorial Team
    Editorial Team added an answer The original VB.NET method is protected, so I guess it's… May 17, 2026 at 2:22 am

Trending Tags

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

Top Members

Related Questions

UPDATED: Added some sample code to help clarify. Hi, Feel like this shouldn't be
I'm pretty good with jQuery but when I get to stuff like this I
I'm having hard time understanding the following C# code. This code was taken from
I need to get pointer to my class instance inside this instance. I can't
I'm using base class constructor as factory and changing class in this constructor/factory to
I've got 3 files that relate to this problem. file.h, file.C and user.C. file.h
I have a question about some functionality I'm trying to add to my jQuery
I have a function which returns only one row as array. I give a
I have some troubles with memory leaks in JavaScript. I'm using the Leak Memory
I am trying to use the IMFSourceResolver::CreateObjectFromByteStream method to create a IMFMediaSource instance for

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.