Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/object-oriented-programming/reset-an-inherited-constructor-property.md
2023-07-24 08:34:47 -07:00

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();