2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7db1367417b2b2512b87
|
|
|
|
|
title: Add Methods After Inheritance
|
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: ''
|
|
|
|
|
localeTitle: 继承后添加方法
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
<section id="description">除了继承的方法之外,从<code>supertype</code>构造函数继承其<code>prototype</code>对象的构造函数仍然可以拥有自己的方法。例如, <code>Bird</code>是一个从<code>Animal</code>继承其<code>prototype</code>的构造函数: <blockquote> function Animal(){} <br> Animal.prototype.eat = function(){ <br> console.log(“nom nom nom”); <br> }; <br>函数Bird(){} <br> Bird.prototype = Object.create(Animal.prototype); <br> Bird.prototype.constructor = Bird; </blockquote>除了从<code>Animal</code>继承的内容之外,您还希望添加<code>Bird</code>对象独有的行为。在这里, <code>Bird</code>将获得一个<code>fly()</code>函数。函数以与任何构造函数相同的方式添加到<code>Bird's</code> <code>prototype</code> : <blockquote> Bird.prototype.fly = function(){ <br> console.log(“我在飞!”); <br> }; </blockquote>现在<code>Bird</code>实例将同时使用<code>eat()</code>和<code>fly()</code>方法: <blockquote> let duck = new Bird(); <br> duck.eat(); //打印“nom nom nom” <br> duck.fly(); //打印“我在飞!” </blockquote></section>
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
<section id="instructions">添加所有必需的代码,以便<code>Dog</code>对象继承自<code>Animal</code> , <code>Dog's</code> <code>prototype</code>构造函数设置为Dog。然后将一个<code>bark()</code>方法添加到<code>Dog</code>对象,以便<code>beagle</code>可以<code>eat()</code>和<code>bark()</code> 。 <code>bark()</code>方法应该打印“Woof!”到控制台。 </section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
|
|
|
|
- text: <code>Animal</code>不应该响应<code>bark()</code>方法。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof Animal.prototype.bark == "undefined");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>Dog</code>应该继承<code>Animal</code>的<code>eat()</code>方法。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(typeof Dog.prototype.eat == "function");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>Dog</code>应该将<code>bark()</code>方法作为<code>own</code>属性。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(Dog.prototype.hasOwnProperty('bark'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>beagle</code>应该是<code>Animal</code>一个<code>instanceof</code> 。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(beagle instanceof Animal);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
- text: <code>beagle</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() { }
|
|
|
|
|
Animal.prototype.eat = function() { console.log("nom nom nom"); };
|
|
|
|
|
|
|
|
|
|
function Dog() { }
|
|
|
|
|
|
|
|
|
|
// Add your code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add your code above this line
|
|
|
|
|
|
|
|
|
|
let beagle = new Dog();
|
|
|
|
|
|
|
|
|
|
beagle.eat(); // Should print "nom nom nom"
|
|
|
|
|
beagle.bark(); // Should print "Woof!"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|
|
|
|
|
</section>
|