4.0 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db1367417b2b2512b87 | Додавання методів після успадкування | 1 | 301315 | add-methods-after-inheritance |
--description--
Функція-конструктор, яка успадковує об’єкт prototype від функції-конструктора супертипу, всеодно може мати власні методи на додачу до успадкованих.
Наприклад, Bird є конструктором, який успадковує prototype від Animal:
function 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(). Функції додаються до прототипу Bird так само, як і будь-яка функція-конструктор:
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
Тепер екземпляри Bird матимуть методи eat() та fly():
let duck = new Bird();
duck.eat();
duck.fly();
duck.eat() показуватиме рядок nom nom nom на консолі, а duck.fly() показуватиме рядок I'm flying!.
--instructions--
Додайте необхідний код так, щоб об’єкт Dog успадковував від Animal, а прототип конструктора Dog був налаштований на Dog. Потім додайте метод bark() до об’єкта Dog, щоб beagle міг eat() та bark(). Метод bark() має вивести Woof! на консоль.
--hints--
Animal не має відповідати на метод bark().
assert(typeof Animal.prototype.bark == 'undefined');
Dog має успадкувати метод eat() від Animal.
assert(typeof Dog.prototype.eat == 'function');
Прототип Dog повинен мати метод bark().
assert('bark' in Dog.prototype);
beagle має бути екземпляром Animal.
assert(beagle instanceof Animal);
Значенням конструктора для beagle має бути Dog.
assert(beagle.constructor === Dog);
beagle.eat() має вивести рядок nom nom nom
capture();
beagle.eat();
uncapture();
assert(logOutput == 'nom nom nom');
beagle.bark() має вивести рядок Woof!
capture();
beagle.bark();
uncapture();
assert(logOutput == 'Woof!');
--seed--
--before-user-code--
var logOutput = "";
var originalConsole = console
function capture() {
var nativeLog = console.log;
console.log = function (message) {
logOutput = message;
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
}
function uncapture() {
console.log = originalConsole.log;
}
capture();
--after-user-code--
uncapture();
(function() { return logOutput || "console.log never called"; })();
--seed-contents--
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Only change code below this line
// Only change code above this line
let beagle = new Dog();
--solutions--
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();