Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách kiểm tra hàm JavaScript đã được định nghĩa chưa

Kiểm tra hàm đã định nghĩa bằng toán tử typeof so với 'undefined'.

Ví dụ

<!DOCTYPE html>
<html>
  <body>
    <script>
      function display() {
        alert("Demo Text!");
      }
      if (typeof display === 'undefined') {
        document.write('undefined');
      } else {
        document.write("Function is defined");
      }
    </script>
  </body>
</html>