Files
2022-01-20 20:30:18 +01:00

2.2 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db1367417b2b2512b86 継承された constructor プロパティを再設定する 1 301324 reset-an-inherited-constructor-property

--description--

オブジェクトが別のオブジェクトから prototype を継承するときは、スーパータイプの constructor プロパティも継承します。

例を示します。

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

しかし、duck と、Bird のすべてのインスタンスでは、それらが Bird によって作成されたものであり、Animal によって作成されたものではないことを示す必要があります。 これを行うには、Bird の constructor プロパティに Bird オブジェクトを手動で設定します。

Bird.prototype.constructor = Bird;
duck.constructor

--instructions--

duck.constructorbeagle.constructor がそれぞれのコンストラクターを返すようにコードを修正してください。

--hints--

Bird.prototypeAnimal のインスタンスである必要があります。

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

duck.constructorBird を返す必要があります。

assert(duck.constructor === Bird);

Dog.prototypeAnimal のインスタンスである必要があります。

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

beagle.constructorDog を返す必要があります。

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