如何检查是否定义了JavaScript函数?
要检查是否定义了JavaScript函数,请使用“undefined”进行检查。
示例
您可以尝试运行以下示例来检查是否在JavaScript中定义了函数-
<!DOCTYPE html> <html> <body> <script> function display() { alert("你好,世界!"); } if ( typeof(display) === 'undefined') { document.write('undefined'); } else { document.write("Function is defined"); } </script> </body> </html>