有没有办法处理JavaScript中调用的未定义函数?

前端之家收集整理的这篇文章主要介绍了有没有办法处理JavaScript中调用的未定义函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我有如下功能
function catchUndefinedFunctionCall( name,arguments )
{
    alert( name + ' is not defined' );
}

而我做的事情很傻

foo( 'bar' );

当没有定义foo时,是否有某些方法可以调用我的catch函数,名称为’foo’,参数是包含’bar’的数组?

解决方法

无论如何都有Mozilla Javascript 1.5(它是非标准的).

看一下这个:

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)

很酷.阅读更多https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/NoSuchMethod

原文链接:https://www.f2er.com/js/159659.html

猜你在找的JavaScript相关文章