Computer >> Máy Tính >  >> Lập trình >> Javascript

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


Sử dụng hàm super () để gọi hàm tạo của lớp cha và truy cập các hàm trên lớp cha của đối tượng.

Ví dụ

Bạn có thể thử chạy mã sau để triển khai super ()

Bản trình diễn trực tiếp

<!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>