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 8149757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:54:55+00:00 2026-06-06T14:54:55+00:00

I have code that looks like this: obj.foo(); // obj might, or might not

  • 0

I have code that looks like this:

obj.foo(); // obj might, or might not have a `foo` method.

I want to know if I can override what happens when obj.foo is called in my code, for example:

obj.foo = function(){ alert ("Hello"); });
obj.onCallNonExistentMethod = function(){ // I know this is imaginary syntax
    alert("World");
}
obj.foo(); // alerts "Hello"
delete obj.foo;
obj.foo(); // alerts "World" , would TypeError without the method missing handler.

From what I understand, in Ruby that would be method_missing or const_missing or something similar.

Can I override what happens on a call to a nonexistent object method in JavaScript? If I can, how do I do it?

The goal is to validate an API I provide to users so they can use the API safely and I can warn them more clearly on errors.

  • 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-06-06T14:54:56+00:00Added an answer on June 6, 2026 at 2:54 pm

    Unfortunately that’s not possible in any widely supported way. The closest you can get is using a proxy function for all calls, i.e. instead of foo.bar() you would use something like foo.invoke('bar'). However, that’s pretty ugly and most likely close to what you meant in the first “not interested in” part.

    In loosely-typed languages is is pretty common to expect the developer to pass compatible objects though – i.e. getting an error when passing something unexpected is usually fine.


    However, if you are lucky enough to use a JS engine which supports ECMAScript-harmony features (obviously IE doesn’t), you can use Proxy objects to achieve this. Basically you wrap your object in a Proxy which lets you trap most operations on the object such as iterating it, retrieving a property, or even creating properties. Besides the two links having a look at this answer to this question might be worth a shot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Conditions: Windows 98 SE WMI not available I have code that looks like this,
In VS 2005, I have some code that looks like this: ifs.open(foo); while (!ifs.eof())
I have a line of code that looks like this: if (obj is byte
I have some code that looks like this: foreach(var obj in collection) { try
I have code that looks like this: template<class T> class list { public: class
I have code that looks like this: class T {}; class container { const
I have code that looks like this: var baseClass = function() { // CODE
I have code that looks something like this in javascript: forloop { //async call,
I have a code that looks like this: int main () { fstream file;
I have a code that looks like this: using (DC dc = new DC())

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.