-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.js
More file actions
58 lines (46 loc) · 1.03 KB
/
inheritance.js
File metadata and controls
58 lines (46 loc) · 1.03 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// inheritance = allows a new class to inherit properties and methods
// from an existing class(parent -> child)
// helps with code reusability.
class Anilmal{
alive = true
eat(){
console.log(`this ${this.name} is eating`);
}
sleep(){
console.log(`this ${this.name} is sleeping`);
}
}
class Rabbit extends Anilmal{
name = 'rabbit'
run(){
console.log(`the ${this.name} can run`);
}
}
class Dog extends Anilmal{
name = "dog"
run(){
console.log(`the ${this.name} can run but can't run faster than rabbit`);
}
}
class Tiger extends Anilmal{
name = "tiger"
run(){
console.log(`the ${this.name} tiger can run very fast`);
}
}
const rabbit = new Rabbit();
const dog = new Dog();
const tiger = new Tiger();
rabbit.alive = false;
console.log(rabbit.alive);
rabbit.eat()
rabbit.sleep()
rabbit.run()
console.log(dog.alive);
dog.eat()
dog.sleep()
dog.run()
console.log(tiger.alive);
tiger.eat()
tiger.sleep()
tiger.run()