在 Javascript 中测试未定义的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6046955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test for undefined function in Javascript
提问by slandau
So Safari keeps yelling at me for one specific error.
所以 Safari 一直因为一个特定的错误而对我大喊大叫。
I'm trying to use Google Maps API and call map.getCenter();
. However sometimes, this happens before the map
has been fully loaded.
我正在尝试使用 Google Maps API 并调用map.getCenter();
. 但是有时,这会在map
完全加载之前发生。
So instead in my function I test for an undefined
call like this:
因此,在我的函数中,我测试了这样的undefined
调用:
if (map.getCenter() != undefined)
But that still errors out because I guess it doesn't even like making the call just to test the if the result is undefined
or not?
但这仍然会出错,因为我想它甚至不喜欢打电话只是为了测试结果是否正确undefined
?
Can I get some help here?
我可以在这里寻求帮助吗?
Thanks!
谢谢!
回答by Swift
I actually prefer something along these lines.
我实际上更喜欢这些方面的东西。
if(typeof myfunc == 'function') {
myfunc();
}
Just because something isn't undefined doesn't make it a function.
仅仅因为某些东西不是 undefined 并不能使它成为一个函数。
回答by Aravindan R
if (typeof map !== 'undefined' && map.getCenter) {
// code for both map and map.getCenter exists
} else {
// if they dont exist
}
This is the right way to check for existence of a function.. Calling the function to test its existence will result in an error.
这是检查函数是否存在的正确方法。调用该函数以测试其存在将导致错误。
UPDATE:Snippet updated.
更新:片段已更新。
回答by Ronny
if (typeof map.getCenter !== 'undefined')
Won't throw an error.
不会抛出错误。
So, better yet, if (typeof map.getCenter === 'function') map.getCenter();
所以,更好的是, if (typeof map.getCenter === 'function') map.getCenter();
回答by matt b
Technically you should be testing if map
is undefined, not map.getCenter()
. You can't call a function on an undefined reference.
从技术上讲,您应该测试是否map
未定义,而不是map.getCenter()
. 您不能在未定义的引用上调用函数。
However, Google's own tutorialsuggests that you invoke your JavaScript that accesses the API in a body.onload handler, so that you do not attempt to reference anything until all of the remote .js files are loaded - are you doing this? This would solve the problem permanently, rather than failing cleanly when your JavaScript executes prior to loading the API.
但是,Google 自己的教程建议您在 body.onload 处理程序中调用访问 API 的 JavaScript,以便在加载所有远程 .js 文件之前不要尝试引用任何内容 - 您这样做了吗?这将永久解决问题,而不是在加载 API 之前执行 JavaScript 时彻底失败。
回答by MacAnthony
I have been in the habit recently of using the typeof operator to test for things and types. It returns the type as a string which I think avoids some response type confusion.
我最近养成了使用 typeof 运算符来测试事物和类型的习惯。它将类型作为字符串返回,我认为这可以避免一些响应类型混淆。
if( typeof map.getCenter != 'undefined') ...
I'm not sure if it's more correct, but I find good results with this process.
我不确定它是否更正确,但我发现这个过程的结果很好。
回答by Ash Burlaczenko
You should be testing whether map
is undefined. In your current check your are still trying to execute the function call.
您应该测试是否map
未定义。在您当前的检查中,您仍在尝试执行函数调用。
回答by Alexander Ryzhov
Assuming that map.getCenter() is always a function, it's sufficient to just use this check:
假设 map.getCenter() 始终是一个函数,只需使用此检查就足够了:
if (map.getCenter) {
var x = map.getCenter();
}