Move sentence and explain constructor rules better in Classes.md - #214
Conversation
| This example covers quite a few of the inheritance features in TypeScript that are common to other languages. | ||
| Here we see the `extends` keywords used to create a subclass. You can see this where `Horse` and `Snake` subclass the base class `Animal` and gain access to its features. | ||
|
|
||
| Derived classes that contain constructor functions must call `super()` which will execute the constructor function on the base class. |
There was a problem hiding this comment.
Can you make this part of the preceding paragraph? I would add two sentences after this one, something like:
You have to call
super()before accessingthisin the derived constructor.
BothHorseandSnaketechnically do this since neither one actually referencesthis.
There was a problem hiding this comment.
Nathan Shively-Sanders (@sandersn) I have added those two sentences. Since you seemed to want to clarify restrictions about constructors, I also added other sentences to explain another restriction from the TypeScript spec about when the 'super' call had to be the first statement in the constructor.
I moved all that into its own paragraph since it got a bit long. It might be too long now, so feel free to suggest further improvements.
I also added a blank line to split up the earlier paragraph, since the sentence about "This example covers quite a few of the inheritance features..." now leads into the three paragraphs that follow which each explain a different concern (extends, constructor restrictions, and overriding).
I left the changes as two separate commits -- the original one to fix an issue and the second one as an enhancement. Let me know if you would prefer them squashed together into one commit though.
8628bec to
1f0d716
Compare
|
|
||
| Here we see the `extends` keywords used to create a subclass. You can see this where `Horse` and `Snake` subclass the base class `Animal` and gain access to its features. | ||
|
|
||
| TypeScript has some restrictions about constructors intended to help ensure instances of a class have their properties correctly initialized before the properties are used. Derived classes that contain constructor functions must call `super()` which will execute the constructor function on the base class. You have to call `super()` before accessing `this` in the derived constructor. Both `Horse` and `Snake` technically do that since neither one actually references `this`. A 'super' call must also be the first statement in the constructor of a derived class when the derived class contains initialized properties or has parameter properties. Neither `Horse` or `Snake`, however, have those kind of properties right now. So, as long as those two classes stay that way, they could have console logging or other statements before the `super` call in their constructors without generating compile-time errors. |
There was a problem hiding this comment.
One sentence per line please. We do this for easy diff-ability.
There was a problem hiding this comment.
Use backticks around "super" in "A 'super' call"
There was a problem hiding this comment.
Thanks for the feedback. I made those improvements and also added parentheses for each super call reference to be consistent.
1f0d716 to
89a00ed
Compare
89a00ed to
14d87d8
Compare
|
👍 I like the changes. We don't care much about commits, so just leave them as-is. |
|
|
||
| Here we see the `extends` keywords used to create a subclass. You can see this where `Horse` and `Snake` subclass the base class `Animal` and gain access to its features. | ||
|
|
||
| TypeScript has some restrictions about constructors intended to help ensure instances of a class have their properties correctly initialized before the properties are used. |
There was a problem hiding this comment.
it is not really a TypeScript restriction. it is a JS one. and enforced by the fact that the super, and the this share the same object, and that the object is not well formed, until super call has returned.
There was a problem hiding this comment.
The text did not make sense with the sentence about calling super() between a sentence that leads into an example (with a colon) and the example.
This issue was introduced in this commit: d0fa464