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

Hàm super() trong JavaScript là gì?

Sử dụng hàm super() để gọi constructor của lớp cha (parent class) và truy cập các phương thức trên đối tượng cha.

Ví dụ

Chạy code dưới để triển khai super():

<!DOCTYPE html>
<html>
  <body>
    <script>
      class Department {
        constructor() {}
        static msg() {
          return 'Hello';
        }
      }
      class Employee extends Department {
        constructor() {}
        static displayMsg() {
          return super.msg() + ' World!';
        }
      }
      document.write(Employee.displayMsg());
    </script>
  </body>
</html>