Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Move sentence and explain constructor rules better in Classes.md #214

Merged
merged 1 commit into from
Apr 26, 2016
Merged
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
4 changes: 2 additions & 2 deletions pages/Classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ tom.move(34);
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 accessing this in the derived constructor.
Both Horse and Snake technically do this since neither one actually references this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.


The example also shows how to override methods in the base class with methods that are specialized for the subclass.
Here both `Snake` and `Horse` create a `move` method that overrides the `move` from `Animal`, giving it functionality specific to each class.
Note that even though `tom` is declared as an `Animal`, since its value is a `Horse`, when `tom.move(34)` calls the overriding method in `Horse`:

Derived classes that contain constructor functions must call `super()` which will execute the constructor function on the base class.

```Text
Slithering...
Sammy the Python moved 5m.
Expand Down