Repository navigation
Expand file tree
/
Copy pathconstructors.js
More file actions
87 lines (69 loc) · 2.09 KB
/
Copy pathconstructors.js
File metadata and controls
87 lines (69 loc) · 2.09 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// to test these problems you can run 'node constructors.js' in your terminal
// problem #1
// add a method to Animal's prototype called 'grow'
// when 'grow' is invoked log '<name> grew larger!'
function Animal(options) {
this.name = options.name;
}
Animal.prototype.grow = function() {
return `${this.name} grew larger!`;
};
const bear = new Animal({ name: 'Stevie Bear' });
console.log('1. Animal.grow()', bear.grow());
// class Animal
// constructor(object) {
// this.type = object.type;
// this.name = object.name;
// this.sound = object.sound;
// }
// speak() {
// return this.sound;
// }
// }
// old constructor function
// function Animal(object) {
// this.type = object.type;
// this.name = object.name;
// this.sound = object.sound;
// this.speak = function() {
// return this.sound;
// };
// }
// const doggo = new Animal({ type: 'dog', name: 'stevie', sound: 'woof!' });
// console.log(doggo.speak());
// const liger = new Animal({
// type: 'Lyger',
// name: 'Leeroy',
// sound: 'ROWADF'
// });
// console.log(liger.speak());
// function Animal(options) {
// this.name = options.name;
// }
// // add 'grow' to Animal's prototype here
// Animal.prototype.grow = function() {
// return `${this.name} grew larger!`;
// };
// const monkey = new Animal('Stevie');
// monkey.grow();
// console.log(monkey);
// problem #2
// setup Cat to inherit from Animal
// the Animal constructor needs to be invoked with the 'options' argument
// Cat should have its prototype inherit from Animal
// instances of Cat should also have access to the 'grow' method
function Cat(options) {
// invoke Animal here with .call
Animal.call(this, options);
}
// inherit from Animal.prototype
Cat.prototype = Object.create(Animal.prototype);
const tabby = new Cat({ name: 'Tabitha' });
console.log('2. Cat inherits from Animal:', tabby.grow());
// connect the prototypes here
// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution
const foofie = new Cat({
name: 'foofie'
});
console.log(foofie.grow());