-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path66 Class Inheritance.html
41 lines (38 loc) · 1.22 KB
/
66 Class Inheritance.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
//A class created with a class is called inheritance and it inherits all the methods from another class:
//basically inheritance is creating a class from another class.
//To create a class inheritance, use the extends keyword.
//Create a class named "Model;" which will inherit the methods from the "Car" class:
//parent class
class Car {
constructor(brand) {
this.carname = brand;
}
present() {
return "I have a " + this.carname;
}
}
//child class
class Model extends Car {
constructor(brand, mod) {
super(brand); //refers to parent
this.model = mod;
}
show() {
return this.present() + ", it is a " + this.model;
}
}
let myCar = new Model("Ford", "Mustang");
console.log(myCar);
//Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.
</script>
</body>
</html>