Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Merged
Changes from 1 commit
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
34 changes: 33 additions & 1 deletion pages/Classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,21 @@ class Animal {

## Understanding `private`

When a member is marked `private`, it cannot be accessed from outside of its containing class. For example:
When a constructor or member is marked `private`, it cannot be accessed from outside of its containing class. For example:

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.

i would move this to a section by itself. a private constructor is not the same as a private property. a class with private constructor can not be extended. so i would add a new paragraph or it, explaining that it can not be called nor extended, and add a static factory function to explain how this pattern would be used.

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.

I agree with Mohamed. I think that this warrants a small section on its own. If you want to open up a separate PR, that might be easier.

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.

Sure, i'll make this PR dedicated to protected then :)

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.

Don't worry about taking care of private. I'll adapt the current content for it.


```ts
class Animal {
private name: string;
constructor(theName: string) { this.name = theName; }
}

class Employee {
private name: string;
private constructor(theName: string) { this.name = theName; }
}

new Animal("Cat").name; // Error: 'name' is private;
new Employee("Bob"); // Error: 'Employee' is private;

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.

The 'Employee' constructor is private.

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.

Don't end these with semicolons

```

TypeScript is a structural type system.
Expand Down Expand Up @@ -189,6 +195,32 @@ console.log(howard.name); // error

Notice that while we can't use `name` from outside of `Person`, we can still use it from within an instance method of `Employee` because `Employee` derives from `Person`.

A constructor may also be marked `protected`. This means that the class cannot be accessed outside of its containing class, but can be extended. For example,

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.

Place sentences on different lines.

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.

not "accessed" but rather "instantiated"

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.

Done


```ts
class Person {
protected name: string;
protected constructor(name: string) { this.name = name; }
}

// Employee can extend Person
class Employee extends Person {
private department: string;

constructor(name: string, department: string) {
super(name);
this.department = department;
}

public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}

let howard = new Employee("Howard", "Sales");
let john = new Person("John"); // Error: 'Person' is protected and only accessible within it's 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.

it's -> its

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.

The 'Person' constructor is protected

```

## Parameter properties

In our last example, we had to declare a private member `name` and a constructor parameter `theName`, and we then immediately set `name` to `theName`.

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.

This is no longer the last example

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.

It seems like that's been the case for quite some time.
Anywho, done!

Expand Down