Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/object-oriented-programming/reset-an-inherited-constructor-property.md

2.1 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b86 Скинути властивість успадкованого конструктора 1 301324 reset-an-inherited-constructor-property

--description--

Якщо об'єкт успадковує prototype від іншого об'єкту, він також успадковує властивість конструктору супертипу.

Наприклад:

function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor

Але duck й усі відбитки Bird повинні відображати, що вони були відтворені в Bird, а не в Animal. Для цього ви можете власноруч встановити властивість конструктора Bird для Bird:

Bird.prototype.constructor = Bird;
duck.constructor

--instructions--

Змініть код, щоб duck.constructor й beagle.constructor повернули їх відповідні конструктори.

--hints--

Bird.prototype повинен бути частиною Animal.

assert(Animal.prototype.isPrototypeOf(Bird.prototype));

duck.constructor має повертати Bird.

assert(duck.constructor === Bird);

Dog.prototype має бути частиною Animal.

assert(Animal.prototype.isPrototypeOf(Dog.prototype));

beagle.constructor має повертати Dog.

assert(beagle.constructor === Dog);

--seed--

--seed-contents--

function Animal() { }
function Bird() { }
function Dog() { }

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

// Only change code below this line



let duck = new Bird();
let beagle = new Dog();

--solutions--

function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Bird.prototype.constructor = Bird;
let duck = new Bird();
let beagle = new Dog();