在进行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. }