mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-05 21:03:24 -05:00
2.0 KiB
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();