1function Animal (name, energy) {
2 this.name = name
3 this.energy = energy
4}
5
6Animal.prototype.eat = function (amount) {
7 console.log(`${this.name} is eating.`)
8 this.energy += amount
9}
10
11Animal.prototype.sleep = function (length) {
12 console.log(`${this.name} is sleeping.`)
13 this.energy += length
14}
15
16Animal.prototype.play = function (length) {
17 console.log(`${this.name} is playing.`)
18 this.energy -= length
19}
20
21function Dog (name, energy, breed) {
22 Animal.call(this, name, energy)
23
24 this.breed = breed
25}
26
27Dog.prototype = Object.create(Animal.prototype)
28
29Dog.prototype.bark = function () {
30 console.log('Woof Woof!')
31 this.energy -= .1
32}
33
34const charlie = new Dog('Charlie', 10, 'Goldendoodle')
35console.log(charlie.constructor)
1// parent class animal
2class Animal {
3 constructor(name, weight) {
4 this.name = name;
5 this.weight = weight;
6 }
7
8 eat() {
9 return `${this.name} is eating!`;
10 }
11
12 sleep() {
13 return `${this.name} is going to sleep!`;
14 }
15
16 wakeUp() {
17 return `${this.name} is waking up!`;
18 }
19
20}
21//sub class gorilla
22
23class Gorilla extends Animal {
24 constructor(name, weight) {
25 super(name, weight);
26 }
27
28 climbTrees() {
29 return `${this.name} is climbing trees!`;
30 }
31
32 poundChest() {
33 return `${this.name} is pounding its chest!`;
34 }
35
36 showVigour() {
37 return `${super.eat()} ${this.poundChest()}`;
38 }
39
40 dailyRoutine() {
41 return `${super.wakeUp()} ${this.poundChest()} ${super.eat()} ${super.sleep()}`;
42 }
43
44}
45
46function display(content) {
47 console.log(content);
48}
49
50const gorilla = new Gorilla('George', '160Kg');
51display(gorilla.poundChest());
52display(gorilla.sleep());
53display(gorilla.showVigour());
54display(gorilla.dailyRoutine());
55
56// OUTPUT:
57// George is pounding its chest!
58// George is going to sleep!
59// George is eating! George is pounding its chest!
60// George is waking up! George is pounding its chest! George is eating! G
61/*
62The above code has 2 JavaScript classes namely Animal and Gorilla.
63The Gorilla class is a subclass or child class of Animal and it uses the extends keyword to set itself as a subclass.
64However, the super keyword has been used in two different ways.
65Did you guys notice the same? In Gorilla’s constructor (line 23 of code)
66super is used as a “function”. Whereas, Gorilla’s showVigour() (line 35) and dailyRoutine()(line 39) methods have used super as an “object”.
67The super keyword is used in two ways because of the following reasons:
68In line 24, the super keyword is used as a “function” which calls the
69parent class Animal with the parameters passed to Gorilla.
70This is a key step to be carried out in order to make sure that
71Gorilla is an instance of Animal.
72In line 36 and 40 super is used as an “object” which
73refers to an Animal instance (parent class). The super keyword
74here is used to call the methods of the parent class Animal explicitly.
75People familiar with languages like C#, Java, Python can pretty
76much relate to how all this works. However, JavaScript was not so simple before ES6 came in, especially for classes. So how did people code without using class syntax, super and extends keywords? Or they never used such concepts before and suddenly decided to add them? Let’s find out!
77*/
1function Person(first, last, age, gender, interests) {
2 this.name = {
3 first,
4 last
5 };
6 this.age = age;
7 this.gender = gender;
8 this.interests = interests;
9};
10
11function Teacher(first, last, age, gender, interests, subject) {
12 Person.call(this, first, last, age, gender, interests);
13
14 this.subject = subject;
15}
1function Animal() { }
2Animal.prototype.eat = function() {
3 return "nom nom nom";
4};
5function Bird() { }
6
7// Inherit all methods from Animal
8Bird.prototype = Object.create(Animal.prototype);
9
10// Bird.eat() overrides Animal.eat()
11Bird.prototype.eat = function() {
12 return "peck peck peck";
13};