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)
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 inherit(c, p) {
2 Object.defineProperty(c, 'prototype', {
3 value: Object.assign(c.prototype, p.prototype),
4 writable: true,
5 enumerable: false
6 });
7
8 Object.defineProperty(c.prototype, 'constructor', {
9 value: c,
10 writable: true,
11 enumerable: false
12 });
13 }
14// Or if you want multiple inheritance
15
16function _inherit(c, ...p) {
17 p.forEach(item => {
18 Object.defineProperty(c, 'prototype', {
19 value: Object.assign(c.prototype, item.prototype),
20 writable: true,
21 enumerable: false
22 });
23 })
24
25 Object.defineProperty(c.prototype, 'constructor', {
26 value: c,
27 writable: true,
28 enumerable: false
29 });
30 }