JavaScript 的 super() 函式是什麼?


可以使用 super() 函式呼叫父類的建構函式,並訪問物件父類上的函式。

示例

你可以嘗試執行以下程式碼來實現 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>

更新於: 08-01-2020

219 次瀏覽

開啟你的 職業 生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.