freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../object-oriented-programming/reset-an-inherited-construc...

2.4 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db1367417b2b2512b86 Reset an Inherited Constructor Property 1 重置继承的构造函数属性

Description

当一个对象从另一个对象继承它的prototype ,它也继承了supertype的构造函数属性。这是一个例子:
函数Bird{}
Bird.prototype = Object.createAnimal.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

修复代码,使duck.constructorbeagle.constructor返回各自的构造函数。

Tests

tests:
  - text: <code>Bird.prototype</code>应该是<code>Animal</code>一个实例。
    testString: 'assert(Animal.prototype.isPrototypeOf(Bird.prototype), "<code>Bird.prototype</code> should be an instance of <code>Animal</code>.");'
  - text: <code>duck.constructor</code>应该返回<code>Bird</code> 。
    testString: 'assert(duck.constructor === Bird, "<code>duck.constructor</code> should return <code>Bird</code>.");'
  - text: <code>Dog.prototype</code>应该是<code>Animal</code>一个实例。
    testString: 'assert(Animal.prototype.isPrototypeOf(Dog.prototype), "<code>Dog.prototype</code> should be an instance of <code>Animal</code>.");'
  - text: <code>beagle.constructor</code>应该返回<code>Dog</code> 。
    testString: 'assert(beagle.constructor === Dog, "<code>beagle.constructor</code> should return <code>Dog</code>.");'

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

// solution required