Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน super() ใน JavaScript คืออะไร?

ใช้ฟังก์ชัน super() เพื่อเรียก Constructor ของคลาสแม่ (Parent Class) และเข้าถึงฟังก์ชันบนคลาสแม่ของออบเจ็กต์

ตัวอย่าง

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