4

I want to be able to do this

var o = {
};
o.functionNotFound(function(name, args) {
  console.log(name + ' does not exist');
});

o.idontexist(); // idontexist does not exist

I think this feature does exist but I can't find it.

fent
  • 17,861
  • 15
  • 87
  • 91

1 Answers1

2

In its current state, JavaScript does not support the exact functionality that you require. The post in the comment gives details on what can and cannot be done. However, if you are willing to forego the use of the “.” method invocation, here’s a code sample that is close to what you want:

var o = 
{
    show: function(m)
    {
        alert(m);
    },

    invoke: function(methname, args)
    {
        try
        {
            this[methname](args);
        }
        catch(e)
        {
            alert("Method '" + methname + "' does not exist");
        }   
    }
}

o.invoke("show", "hello");
o.invoke("sho", "hello");

Output:

hello

Method 'sho' does not exist

Abbas
  • 6,720
  • 4
  • 35
  • 49