--- id: 587d7db1367417b2b2512b87 title: Add Methods After Inheritance challengeType: 1 forumTopicId: 301315 localeTitle: Добавить методы после наследования --- ## Description
Функция-конструктор, которая наследует свой объект- prototype от функции-конструктора supertype может по-прежнему иметь свои собственные методы в дополнение к унаследованным методам. Например, Bird - это конструктор, который наследует свой prototype от Animal :
функция Animal () {}
Animal.prototype.eat = function () {
console.log («nom nom nom»);
};
function Bird () {}
Bird.prototype = Object.create (Animal.prototype);
Bird.prototype.constructor = Bird;
В дополнение к тому, что унаследовано от Animal , вы хотите добавить поведение, уникальное для объектов Bird . Здесь Bird получит функцию fly() . Функции добавляются к prototype Bird's же, как и любая функция конструктора:
Bird.prototype.fly = function () {
console.log («Я летаю!»);
};
Теперь экземпляры Bird будут иметь методы eat() и fly() :
let duck = new Bird ();
duck.eat (); // печатает "nom nom nom"
duck.fly (); // печатает «Я лечу!»
## Instructions
Добавьте все необходимые символы , поэтому Dog объект наследует от Animal и в Dog's prototype конструктора установлена Собаке. Затем добавьте метод bark() к объекту Dog чтобы beagle мог как eat() и bark() . Метод bark() должен печатать «Woof!» на консоль.
## Tests
```yml tests: - text: Animal should not respond to the bark() method. testString: assert(typeof Animal.prototype.bark == "undefined"); - text: Dog should inherit the eat() method from Animal. testString: assert(typeof Dog.prototype.eat == "function"); - text: Dog should have the bark() method as an own property. testString: assert(Dog.prototype.hasOwnProperty('bark')); - text: beagle should be an instanceof Animal. testString: assert(beagle instanceof Animal); - text: The constructor for beagle should be set to Dog. testString: assert(beagle.constructor === Dog); ```
## Challenge Seed
```js 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
```js 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(); ```