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// Class Inheritance in JavaScript
2class Mammal {
3 constructor(name) {
4 this.name = name;
5 }
6 eats() {
7 return `${this.name} eats Food`;
8 }
9}
10
11class Dog extends Mammal {
12 constructor(name, owner) {
13 super(name);
14 this.owner = owner;
15 }
16 eats() {
17 return `${this.name} eats Chicken`;
18 }
19}
20
21let myDog = new Dog("Spot", "John");
22console.log(myDog.eats()); // Spot eats chicken
1function Teacher(first, last, age, gender, interests, subject) {
2 Person.call(this, first, last, age, gender, interests);
3
4 this.subject = subject;
5}