有没有办法处理JavaScript中调用的未定义函数? [英] Is there a way to handle undefined functions being called in JavaScript?

查看:174
本文介绍了有没有办法处理JavaScript中调用的未定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有如下功能:

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

我做一些傻事像

foo( 'bar' );



< foo和参数是包含bar的数组?

when foo isn't defined, is there some way I can have my catch function called, with name being 'foo' and arguments being an array containing 'bar'?

推荐答案

还有Mozilla Javascript 1.5(非标准)

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

检查出来:

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

这篇关于有没有办法处理JavaScript中调用的未定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆