2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 587d7db1367417b2b2512b87
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 继承后添加方法
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-08-04 07:15:28 +00:00
|
|
|
|
forumTopicId: 301315
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: add-methods-after-inheritance
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
从超类构造函数继承其 `prototype` 对象的构造函数,除了继承的方法外,还可以拥有自己的方法。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
请看举例:`Bird` 是一个构造函数,它继承了 `Animal` 的 `prototype`:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function Animal() { }
|
|
|
|
|
Animal.prototype.eat = function() {
|
|
|
|
|
console.log("nom nom nom");
|
|
|
|
|
};
|
|
|
|
|
function Bird() { }
|
|
|
|
|
Bird.prototype = Object.create(Animal.prototype);
|
|
|
|
|
Bird.prototype.constructor = Bird;
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
除了从 `Animal` 构造函数继承的行为之外,还需要给 `Bird` 对象添加它独有的行为。 这里,我们给 `Bird` 对象添加一个 `fly()` 函数。 函数会以一种与其他构造函数相同的方式添加到 `Bird's` 的 `prototype` 中:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
Bird.prototype.fly = function() {
|
|
|
|
|
console.log("I'm flying!");
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
现在 `Bird` 的实例中就有了 `eat()` 和 `fly()` 这两个方法:
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let duck = new Bird();
|
2021-03-15 03:20:39 +00:00
|
|
|
|
duck.eat();
|
|
|
|
|
duck.fly();
|
2020-08-04 07:15:28 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`duck.eat()` 将在控制台中显示字符串 `nom nom nom`, `duck.fly()` 将显示字符串 `I'm flying!`。
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
添加必要的代码,使得 `Dog` 对象继承 `Animal`,并且把 `Dog` 的 `prototype`上的 constructor 属性设置为 `Dog`。 然后给 `Dog` 对象添加一个 `bark()` 方法,这样的话,`beagle` 将同时拥有 `eat()` 和 `bark()` 这两个方法。 `bark()` 方法中应该输出 `Woof!` 到控制台。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`Animal` 应该没有 `bark()` 方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(typeof Animal.prototype.bark == 'undefined');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`Dog` 应该继承了 `Animal` 的 `eat()` 方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof Dog.prototype.eat == 'function');
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`Dog` 应该有一个 `bark()` 方法作为 `own` 属性。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(Dog.prototype.hasOwnProperty('bark'));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`beagle` 应该是 `Animal` 的一个 `instanceof`。
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(beagle instanceof Animal);
|
|
|
|
|
```
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`beagle` 的 constructor 属性应该被设置为 `Dog`。
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(beagle.constructor === Dog);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-04 07:15:28 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`beagle.eat()` 应该记录字符串 `nom nom nom`
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
console.log = function (msg) {
|
|
|
|
|
throw msg;
|
|
|
|
|
};
|
|
|
|
|
assert.throws(() => beagle.eat(), 'nom nom nom');
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`beagle.bark()` 应该将字符串 `Woof!` 打印到控制台
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
console.log = function (msg) {
|
|
|
|
|
throw msg;
|
|
|
|
|
};
|
|
|
|
|
assert.throws(() => beagle.bark(), 'Woof!');
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function Animal() { }
|
|
|
|
|
Animal.prototype.eat = function() { console.log("nom nom nom"); };
|
|
|
|
|
|
|
|
|
|
function Dog() { }
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
|
|
|
|
|
let beagle = new Dog();
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
function Animal() { }
|
|
|
|
|
Animal.prototype.eat = function() { console.log("nom nom nom"); };
|
|
|
|
|
|
|
|
|
|
function Dog() { }
|
|
|
|
|
Dog.prototype = Object.create(Animal.prototype);
|
|
|
|
|
Dog.prototype.constructor = Dog;
|
|
|
|
|
Dog.prototype.bark = function () {
|
|
|
|
|
console.log('Woof!');
|
|
|
|
|
};
|
|
|
|
|
let beagle = new Dog();
|
|
|
|
|
|
|
|
|
|
beagle.eat();
|
|
|
|
|
beagle.bark();
|
|
|
|
|
```
|