2.6 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7db1367417b2b2512b86 Reset an Inherited Constructor Property 1 301324 Сбросить свойство унаследованного конструктора

Description

Когда объект наследует свой prototype от другого объекта, он также наследует свойство конструктора supertype . Вот пример:
function Bird () {}
Bird.prototype = Object.create (Animal.prototype);
let duck = new Bird ();
duck.constructor // function Animal () {...}
Но duck и все случаи Bird должны показать, что они были построены Bird а не Animal . Для этого, вы можете вручную установить Bird's свойство конструктора для Bird объекта:
Bird.prototype.constructor = Bird;
duck.constructor // function Bird () {...}

Instructions

Fix the code so duck.constructor and beagle.constructor return their respective constructors.

Tests

tests:
  - text: <code>Bird.prototype</code> should be an instance of <code>Animal</code>.
    testString: assert(Animal.prototype.isPrototypeOf(Bird.prototype));
  - text: <code>duck.constructor</code> should return <code>Bird</code>.
    testString: assert(duck.constructor === Bird);
  - text: <code>Dog.prototype</code> should be an instance of <code>Animal</code>.
    testString: assert(Animal.prototype.isPrototypeOf(Dog.prototype));
  - text: <code>beagle.constructor</code> should return <code>Dog</code>.
    testString: assert(beagle.constructor === Dog);

Challenge Seed

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

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

// Add your code below this line



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

Solution

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