Files
freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-and-data-structures/object-oriented-programming/reset-an-inherited-constructor-property.md
2023-01-19 16:02:13 +01:00

2.0 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b86 إعادة تعيين خاصية موروثة 1 301324 reset-an-inherited-constructor-property

--description--

عندما يرث الـ object الـ prototype من object آخر، فإنه يرث أيضا الـ supertype's constructor property.

على سبيل المثال:

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

ولكن duck وجميع الـ instances من Bird يجب أن تظهر أنها بنيت اي constructed، بواسطة Bird وليس Animal. للقيام بذلك، يمكنك تعيين constructor property لـ Bird إلى object الـ Bird:

Bird.prototype.constructor = Bird;
duck.constructor

--instructions--

أصلح الكود بحيث يقوم duck.constructor و beagle.constructor بإرجاع الـ constructors لكل منهم.

--hints--

Bird.prototype يجب أن يكون instance لـ Animal.

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

duck.constructor يجب أن يرجع Bird.

assert(duck.constructor === Bird);

Dog.prototype يجب أن يكون instance لـ 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();