Skip to content

Commit d1466aa

Browse files
SunJieMingSunJieMing
authored andcommitted
Add solution
1 parent 5f4a5bc commit d1466aa

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

classes.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22

33
// problem #1
44
// convert the Animal constructor function from 'constructors.js' into an ES6 class
5+
class Animal {
6+
constructor(options) {
7+
this.name = options.name;
8+
}
9+
grow() {
10+
console.log(`${this.name} grew larger!`);
11+
}
12+
}
513

614

715
// problem #2
816
// convert the Cat constructor function from 'constructors.js' into an ES6 class
917

18+
class Cat extends Animal {
19+
meow() {
20+
console.log('meow!');
21+
}
22+
}
1023

1124
// if everything is setup properly the code below will print 'Foofie grew larger!'
1225
// uncomment the code below to test your solution
1326

14-
// const foofie = new Cat({
15-
// name: 'foofie',
16-
// });
17-
//
18-
// foofie.grow();
27+
const foofie = new Cat({
28+
name: 'foofie',
29+
});
1930

31+
foofie.grow();
32+
foofie.meow();

constructors.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function Animal(options) {
88
this.name = options.name;
99
}
1010

11+
Animal.prototype.grow = function() {
12+
console.log(`${this.name} grew larger!`);
13+
};
14+
1115
// add 'grow' to Animal's prototype here
1216

1317
// problem #2
@@ -17,17 +21,20 @@ function Animal(options) {
1721
// instances of Cat should also have access to the 'grow' method
1822

1923
function Cat(options) {
24+
Animal.call(this, options);
2025
// invoke Animal here with .call
2126
}
2227

28+
Cat.prototype = Object.create(Animal.prototype);
29+
2330
// connect the prototypes here
2431

2532
// if everything is setup properly the code below will print 'Foofie grew larger!'
2633
// uncomment the code below to test your solution
2734

28-
// const foofie = new Cat({
29-
// name: 'foofie',
30-
// });
31-
//
32-
// foofie.grow();
35+
const foofie = new Cat({
36+
name: 'foofie',
37+
});
38+
39+
foofie.grow();
3340

0 commit comments

Comments
 (0)