Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/object-oriented-programming/override-inherited-methods.md

3.1 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b88 Змінити успадковані методи 1 301322 override-inherited-methods

--description--

У попередніх уроках ви дізналися, що об'єкт може успадковувати поведінку (методи) від іншого об'єкту, посилаючись на його prototype:

ChildObject.prototype = Object.create(ParentObject.prototype);

Тоді ChildObject отримує власні методи завдяки привласненню їх prototype:

ChildObject.prototype.methodName = function() {...};

Успадкований метод можна змінити. Це робиться так само: треба додати метод до ChildObject.prototype, використовуючи таку ж саму назву методу, як й у того, що підлягає редагуванню. Ось приклад Bird зі змінним eat() методом, успадкованим від Animal:

function Animal() { }
Animal.prototype.eat = function() {
  return "nom nom nom";
};
function Bird() { }

Bird.prototype = Object.create(Animal.prototype);

Bird.prototype.eat = function() {
  return "peck peck peck";
};

Якщо у вас є let duck = new Bird();, викличте duck.eat() — таким чином використовується JavaScript для методу prototype ланцюга duck:

  1. duck => Is eat() визначений у цьому випадку? Ні.
  2. Bird => Is eat() визначений у цьому випадку? => Так. Виконайте це й припиніть пошуки.
  3. Animal => eat() також визначено, але JavaScript вже припинив пошук перед цим рівнем.
  4. Object => JavaScript припинив пошук до досягнення цього рівня.

--instructions--

Замінить fly() метод для Penguin повернення рядку Alas, this is a flightless bird.

--hints--

penguin.fly() має повернути рядок Alas, this is a flightless bird.

assert(penguin.fly() === 'Alas, this is a flightless bird.');

Метод bird.fly() має повернути рядок I am flying!

assert(new Bird().fly() === 'I am flying!');

--seed--

--seed-contents--

function Bird() { }

Bird.prototype.fly = function() { return "I am flying!"; };

function Penguin() { }
Penguin.prototype = Object.create(Bird.prototype);
Penguin.prototype.constructor = Penguin;

// Only change code below this line



// Only change code above this line

let penguin = new Penguin();
console.log(penguin.fly());

--solutions--

function Bird() { }

Bird.prototype.fly = function() { return "I am flying!"; };

function Penguin() { }
Penguin.prototype = Object.create(Bird.prototype);
Penguin.prototype.constructor = Penguin;
Penguin.prototype.fly = () => 'Alas, this is a flightless bird.';
let penguin = new Penguin();
console.log(penguin.fly());