[转]js 判断js函数、变量是否存在

本文转自:http://blog.csdn.net/liang4571231/article/details/4042519

在进行js编程时,总会出现可能一些函数或者变量未定义而被引用,导致报错的情况。为了避免此类事情的发生,可以在调用前判断函数是否已经被定义。

函数:

 
  1. try 
  2. {  
  3. if(typeof(eval(funcName))=="function")  
  4. {  
  5. funcName();  
  6. }  
  7. }catch(e)  
  8. {  
  9. alert("not function");  
  10. }   

 

变量:

 
  1. function check()  
  2. {  
  3. if (typeof(myvalue)=="undefined")  
  4. {  
  5. alert("value is undefined");  
  6. }  
  7. else 
  8. {  
  9. alert("value is true");  
  10. }  
  11. }   

 

posted on 2013-10-07 10:05  freeliver54  阅读(8984)  评论(0编辑  收藏  举报

导航