Files
freeCodeCamp/curriculum/challenges/german/02-javascript-algorithms-and-data-structures/object-oriented-programming/reset-an-inherited-constructor-property.md
2022-08-19 20:53:29 +02:00

1.9 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b86 Setze eine geerbte Konstruktoreigenschaft zurück 1 301324 reset-an-inherited-constructor-property

--description--

Wenn ein Objekt seinen prototype von einem anderen Objekt erbt, erbt es auch die Konstruktoreigenschaft des Supertyps.

Hier ist ein Beispiel:

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

Aber duck und alle Instanzen von Bird sollten zeigen, dass sie von Bird und nicht von Animal erstellt wurden. Dazu kannst du die Konstruktor-Eigenschaft von Bird manuell auf das Bird-Objekt setzen:

Bird.prototype.constructor = Bird;
duck.constructor

--instructions--

Korrigiere den Code, damit duck.constructor und beagle.constructor ihre jeweiligen Konstruktoren zurückgeben.

--hints--

Bird.prototype sollte eine Instanz von Animal sein.

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

Der duck.constructor sollte Bird zurückgeben.

assert(duck.constructor === Bird);

Dog.prototype sollte eine Instanz von Animal sein.

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

beagle.constructor sollte Dog zurückgeben.

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