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};
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 }
1function Teacher(first, last, age, gender, interests, subject) {
2 Person.call(this, first, last, age, gender, interests);
3
4 this.subject = subject;
5}