1-js/07-object-oriented-programming/10-class-inheritance#138
Merged
leviding merged 8 commits intojavascript-tutorial:zh-hansfrom Aug 13, 2018
Merged
1-js/07-object-oriented-programming/10-class-inheritance#138leviding merged 8 commits intojavascript-tutorial:zh-hansfrom
leviding merged 8 commits intojavascript-tutorial:zh-hansfrom
Conversation
Contributor
|
@leviding 校对认领 |
Member
|
@Starriers ok |
Starriers
reviewed
Aug 9, 2018
| Here's the code with `Rabbit` extending `Animal`. | ||
| 这里有一份代码,是 `Rabbit` 继承 `Animal`。 | ||
|
|
||
| 不幸的是,`Rabbit` 无法被创建,是哪里出错了呢?请解决这个问题 |
|
|
||
| The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined". | ||
| 如果我们尝试运行它,就会发现明显的原因。派生类的构造函数必须调用 | ||
| `super()`。否则不会定义 `"this"`。 |
Contributor
There was a problem hiding this comment.
这是同一句啊 不用换行
this 不是一直有啊 还是直译吧
| constructor(name) { | ||
| *!* | ||
| super(); // need to call the parent constructor when inheriting | ||
| super(); // 需要在继承是调用父类的构造函数 |
| */!* | ||
| ``` | ||
|
|
||
| But if we don't have `extends Object`, then `Rabbit.__proto__` is not set to `Object`. |
| @@ -4,9 +4,9 @@ importance: 5 | |||
|
|
|||
| # Class extends Object? | |||
|
|
||
| *!* | ||
| rabbit.eat(); // Error calling super (because there's no [[HomeObject]]) | ||
| rabbit.eat(); // 调用报错(因为没有 [[HomeObject]]) |
| 每个方法都会在内部的 `[[HomeObject]]` 属性上标记它的对象。然后 `super` 利用它来解析父级原型。 | ||
|
|
||
|
|
||
| `[[HomeObject]]` 是为类和纯对象中定义的方法定义的。但是对于对象,方法必须按照给定的方式定义:使用 `method()`,而不是 `"method: function()"`。 |
| __proto__: animal, | ||
| eat() { | ||
| // ...bounce around rabbit-style and call parent (animal) method | ||
| // 新建一个兔子并调用父类的方法 |
| __proto__: rabbit, | ||
| eat() { | ||
| // ...do something with long ears and call parent (rabbit) method | ||
| // 用长耳朵做一些事情,并调用父类(rabbit)的方法 |
| ``` | ||
|
|
||
| 3. ...So `rabbit.eat` calls itself in the endless loop, because it can't ascend any further. | ||
| 3. 所以 `rabbit.eat` 不停地循环调用自己,因此它无法进一步地往原型链的更高层调用 |
Member
|
@zhangfe 修改下吧 |
Member
|
@zhangfe 麻烦尽快修改下哈 |
Member
|
@zhangfe 请马上修改 |
Contributor
Author
|
@leviding done,已经修改了 |
Member
|
整体很不错,细节格式再注意下就更棒了 |
Contributor
Author
|
@leviding 不好意思,我下次注意,哈哈;这个还用分享到掘金去嘛? |
Contributor
|
@zhangfe 不用 这个仓库是单独的 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
翻译完成 #104