Repository navigation
Expand file tree
/
Copy pathanimal.js
More file actions
57 lines (45 loc) · 1.47 KB
/
Copy pathanimal.js
File metadata and controls
57 lines (45 loc) · 1.47 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
// 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!'
// Prototype is an SPECIAL Obj that is assigned objects when they are created
// We use the prototype to pass on abilities to child objects;
////// pseudoclassical inheritence ///////////
function Animal(options) {
this.species = options.species;
this.name = options.name;
}
Animal.prototype.grow = function() {
console.log(`${this.name} grew larger`);
};
function Dog(dogAttributes) {
Animal.call(this, dogAttributes);
this.speak = dogAttributes.speak;
this.waggyTail = dogAttributes.waggyTail;
}
Dog.prototype = Object.create(Animal.prototype);
const grizzly = new Dog({
species: 'Canis cuteus',
name: 'Grizzly Bear',
speak: 'woof',
waggyTail: true,
});
grizzly.grow();
console.log(grizzly);
// add 'grow' to Animal's prototype here
// 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
}
// 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',
// });
//
// foofie.grow();