-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_addMethodsAfterInheritance.js
More file actions
46 lines (36 loc) · 1.23 KB
/
21_addMethodsAfterInheritance.js
File metadata and controls
46 lines (36 loc) · 1.23 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
// Add Methods After Inheritance
/* A constructor function that inherits its prototype object from a
supertype constructor function can still have its own methods in
addition to inherited methods. */
function Animal() {}
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Bird() {}
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
// To add a behaviour unique to Bird objects:
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
let duck = new Bird();
duck.eat(); // nom nom nom
duck.fly(); // I'm flying!
/* Add all necessary code so the Dog object inherits from
Animal and the Dog's prototype constructor is set to Dog.
Then add a bark() method to the Dog object so that beagle
can both eat() and bark(). The bark() method should print
Woof! to the console. */
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Only change code below this line
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
console.log("Woof!");
}
// Only change code above this line
let beagle = new Dog();
beagle.eat(); // nom nom nom
beagle.bark(); // Woof!