Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Here's the line with the error:
这里有一行报错代码:

```js
Rabbit.prototype = Animal.prototype;
```

Here `Rabbit.prototype` and `Animal.prototype` become the same object. So methods of both classes become mixed in that object.
这里 `Rabbit.prototype` `Animal.prototype` 变成了同样的对象。所以这两个类的方法都混合在了这个对象中。

As a result, `Rabbit.prototype.walk` overwrites `Animal.prototype.walk`, so all animals start to bounce:
结果, `Rabbit.prototype.walk` 重写了 `Animal.prototype.walk`,所以所有动物都开始跳:

```js run
function Animal(name) {
Expand Down Expand Up @@ -35,12 +35,12 @@ animal.walk(); // pig bounces!
*/!*
```

The correct variant would be:
正确的变体是:

```js
Rabbit.prototype.__proto__ = Animal.prototype;
// or like this:
Rabbit.prototype = Object.create(Animal.prototype);
```

That makes prototypes separate, each of them stores methods of the corresponding class, but `Rabbit.prototype` inherits from `Animal.prototype`.
这使原型分开,每个原型都存储相应类的方法,但是 `Rabbit.prototype` 继承自 `Animal.prototype`
Morty Proxy This is a proxified and sanitized view of the page, visit original site.