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
Nathan Shively-Sanders (sandersn) merged 1 commit into
microsoft:masterfrom
pdfernhout:classes.md-sentence-order-fix
Apr 26, 2016
Merged

Move sentence and explain constructor rules better in Classes.md#214
Nathan Shively-Sanders (sandersn) merged 1 commit into
microsoft:masterfrom
pdfernhout:classes.md-sentence-order-fix

Conversation

@pdfernhout

Copy link
Copy Markdown
Contributor

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

Comment thread pages/Classes.md
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@pdfernhout
Paul D. Fernhout (pdfernhout) force-pushed the classes.md-sentence-order-fix branch 7 times, most recently from 8628bec to 1f0d716 Compare March 30, 2016 02:15
Comment thread pages/Classes.md Outdated

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One sentence per line please. We do this for easy diff-ability.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use backticks around "super" in "A 'super' call"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I made those improvements and also added parentheses for each super call reference to be consistent.

@pdfernhout Paul D. Fernhout (pdfernhout) changed the title Move sentence in Classes.md Move sentence and explain constructor rules better in Classes.md Mar 30, 2016
@sandersn

Copy link
Copy Markdown
Member

👍 I like the changes. We don't care much about commits, so just leave them as-is.

Comment thread pages/Classes.md Outdated

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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