2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db1367417b2b2512b86
|
|
|
|
challengeType: 1
|
2020-08-04 15:15:28 +08:00
|
|
|
forumTopicId: 301324
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 重置一个继承的构造函数属性
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:15:28 +08:00
|
|
|
<section id='description'>
|
|
|
|
当一个对象从另一个对象那里继承了其<code>原型</code>,那它也继承了<code>父类</code>的 constructor 属性。
|
|
|
|
请看下面的举例:
|
|
|
|
|
|
|
|
```js
|
|
|
|
function Bird() { }
|
|
|
|
Bird.prototype = Object.create(Animal.prototype);
|
|
|
|
let duck = new Bird();
|
|
|
|
duck.constructor // function Animal(){...}
|
|
|
|
```
|
|
|
|
|
|
|
|
但是<code>duck</code>和其他所有<code>Bird</code>的实例都应该表明它们是由<code>Bird</code>创建的,而不是由<code>Animal</code>创建的。为此,你可以手动把<code>Bird</code>的 constructor 属性设置为<code>Bird</code>对象:
|
|
|
|
|
|
|
|
```js
|
|
|
|
Bird.prototype.constructor = Bird;
|
|
|
|
duck.constructor // function Bird(){...}
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:15:28 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
修改你的代码,使得<code>duck.constructor</code>和<code>beagle.constructor</code>返回各自的构造函数。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-04 15:15:28 +08:00
|
|
|
- text: <code>Bird.prototype</code>应该是<code>Animal</code>的一个实例。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(Animal.prototype.isPrototypeOf(Bird.prototype));
|
2020-08-04 15:15:28 +08:00
|
|
|
- text: <code>duck.constructor</code>应该返回<code>Bird</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(duck.constructor === Bird);
|
2020-08-04 15:15:28 +08:00
|
|
|
- text: <code>Dog.prototype</code>应该是<code>Animal</code>的一个实例。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(Animal.prototype.isPrototypeOf(Dog.prototype));
|
2020-08-04 15:15:28 +08:00
|
|
|
- text: <code>beagle.constructor</code>应该返回<code>Dog</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(beagle.constructor === Dog);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
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();
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-08-04 15:15:28 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-08-04 15:15:28 +08:00
|
|
|
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();
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:15:28 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|