Is there a way to handle undefined functions being called in JavaScript?

后端 未结 2 1297
梦如初夏
梦如初夏 2021-02-15 03:20

If I have a function like the following:

function catchUndefinedFunctionCall( name, arguments )
{
    alert( name + \' is not defined\' );
}

an

相关标签:
2条回答
  • 2021-02-15 03:33
    try {
     foo();
    }
    catch(e) {
       callUndefinedFunctionCatcher(e.arguments);
    }
    

    UPDATED

    passing e.arguments to your function will give you what you tried to pass originally.

    0 讨论(0)
  • 2021-02-15 03:45

    There is in Mozilla Javascript 1.5 anyway (it's nonstandard).

    Check this out:

    var myObj = {
        foo: function () {
            alert('foo!');
        }
        , __noSuchMethod__: function (id, args) {
            alert('Oh no! '+id+' is not here to take care of your parameter/s ('+args+')');
        } 
    }
    myObj.foo();
    myObj.bar('baz', 'bork'); // => Oh no! bar is not here to take care of your parameter/s (baz,bork)
    

    Pretty cool. Read more at https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/NoSuchMethod

    0 讨论(0)
提交回复
热议问题