<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script>
class Father {
constructor(x, y) {
this.x = x;
this.y = y;
}
sum() {
console.log(this.x + this.y);
}
}
// 子類繼父親 加法同時擴展減法方法
class Son extends Father {
constructor(x, y) {
// 利用super調用父類的構造函數
// ❤️super必須在子類this之前調用
super(x, y);
this.x = x;
this.y = y;
}
subtract() {
console.log(this.x - this.y);
}
}
var son = new Son(5, 3);
son.subtract();
son.sum();
// 💗注意:子類在構造函數中使用super,必須放在this前面,(必須先調用父類的構造方法在使用子類構造方法)
</script>
</body>
</html>