3.7 KiB
3.7 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7db1367417b2b2512b87 | Add Methods After Inheritance | 1 |
Description
prototype
object from a supertype
constructor function can still have its own methods in addition to inherited methods.
For example, Bird
is a constructor that inherits its prototype
from Animal
:
function Animal() { }In addition to what is inherited from
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
Animal
, you want to add behavior that is unique to Bird
objects. Here, Bird
will get a fly()
function. Functions are added to Bird's
prototype
the same way as any constructor function:
Bird.prototype.fly = function() {Now instances of
console.log("I'm flying!");
};
Bird
will have both eat()
and fly()
methods:
let duck = new Bird();
duck.eat(); // prints "nom nom nom"
duck.fly(); // prints "I'm flying!"
Instructions
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.
Tests
tests:
- text: <code>Animal</code> should not respond to the <code>bark()</code> method.
testString: 'assert(typeof Animal.prototype.bark == "undefined", "<code>Animal</code> should not respond to the <code>bark()</code> method.");'
- text: <code>Dog</code> should inherit the <code>eat()</code> method from <code>Animal</code>.
testString: 'assert(typeof Dog.prototype.eat == "function", "<code>Dog</code> should inherit the <code>eat()</code> method from <code>Animal</code>.");'
- text: <code>Dog</code> should have the <code>bark()</code> method as an <code>own</code> property.
testString: 'assert(Dog.prototype.hasOwnProperty("bark"), "<code>Dog</code> should have the <code>bark()</code> method as an <code>own</code> property.");'
- text: <code>beagle</code> should be an <code>instanceof</code> <code>Animal</code>.
testString: 'assert(beagle instanceof Animal, "<code>beagle</code> should be an <code>instanceof</code> <code>Animal</code>.");'
- text: The constructor for <code>beagle</code> should be set to <code>Dog</code>.
testString: 'assert(beagle.constructor === Dog, "The constructor for <code>beagle</code> should be set to <code>Dog</code>.");'
Challenge Seed
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Add your code below this line
// Add your code above this line
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"
Solution
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function () {
console.log('Woof!');
};
let beagle = new Dog();
beagle.eat();
beagle.bark();