Skip to content

Commit 404f99d

Browse files
committed
fix: remove access modifiers, use native #private fields
BREAKING CHANGE: Access modifiers exercise completely removed - Delete 02.access-modifiers exercise entirely (public/private/protected keywords removed from curriculum) - Renumber exercises: interfaces-and-classes → 02, inheritance → 03, polymorphism → 04, composition-vs-inheritance → 05 - Integrate #private fields (native JavaScript private) into 01.classes exercise as the preferred encapsulation pattern - Update all code examples to use #private instead of TypeScript private - Update README and descriptions to reflect new structure
1 parent e78f5ec commit 404f99d

File tree

72 files changed

+79
-361
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+79
-361
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Build robust applications with classes, inheritance, and design patterns
55
</strong>
66
<p>
7-
Learn classes, access modifiers, inheritance, polymorphism, and when to choose composition over inheritance.
7+
Learn classes, inheritance, polymorphism, and when to choose composition over inheritance.
88
</p>
99
</div>
1010

exercises/01.classes/02.problem.constructors/README.mdx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,47 @@ class User {
1818
}
1919
```
2020

21+
## Private Fields
22+
23+
JavaScript has native private fields using the `#` prefix:
24+
25+
```ts
26+
class BankAccount {
27+
#balance: number = 0 // Truly private - not accessible outside the class
28+
29+
deposit(amount: number) {
30+
this.#balance += amount
31+
}
32+
}
33+
34+
const account = new BankAccount()
35+
account.#balance // ❌ Error! Private field not accessible
36+
```
37+
2138
🐨 Open <InlineFile file="index.ts" /> and:
2239

23-
1. Refactor classes to use parameter properties
40+
1. Create classes using parameter property shorthand
2441
2. Use default parameter values
25-
3. Make some parameters optional
42+
3. Use `#` prefix for fields that should be private
43+
44+
💰 Parameter property shorthand:
45+
46+
```ts
47+
constructor(public name: string, public email: string) {}
48+
```
2649

27-
💰 Parameter property syntax:
50+
💰 Private field syntax:
2851

2952
```ts
30-
constructor(public name: string, private age: number = 0) {}
53+
class Counter {
54+
#count: number = 0
55+
56+
increment() {
57+
this.#count++
58+
}
59+
}
3160
```
3261

33-
📜 [TypeScript Handbook - Parameter Properties](https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties)
62+
📜 [MDN - Private class features](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields)
3463

3564
🐨 Once you've completed the exercise, run `node index.ts` in the playground to test your work!

exercises/01.classes/02.problem.constructors/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
// Constructor Patterns
22

3-
// 🐨 Create a User class using parameter properties:
4-
// - public name: string
5-
// - public email: string
6-
// - public role: string (default: 'user')
3+
// 🐨 Create a User class with these fields:
4+
// - name: string
5+
// - email: string
6+
// - role: string (default: 'user')
7+
// Use constructor parameter shorthand!
78

89
// 🐨 Create a BankAccount class with:
9-
// - public accountNumber: string
10-
// - private balance: number (default: 0)
10+
// - accountNumber: string
11+
// - #balance: number (private field, default: 0)
1112
// - Method: deposit(amount: number)
1213
// - Method: getBalance() returns the balance
14+
// 💰 #balance is a private field - can only be accessed inside the class
1315

14-
// 🐨 Create a Config class with optional properties:
15-
// - public host: string (default: 'localhost')
16-
// - public port: number (default: 3000)
17-
// - public debug: boolean (default: false)
16+
// 🐨 Create a Config class with optional default values:
17+
// - host: string (default: 'localhost')
18+
// - port: number (default: 3000)
19+
// - debug: boolean (default: false)
1820

1921
// Test
2022
// const user = new User('Alice', '[email protected]')

exercises/01.classes/02.solution.constructors/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ export class User {
99
}
1010

1111
export class BankAccount {
12-
constructor(
13-
public accountNumber: string,
14-
private balance: number = 0,
15-
) {}
12+
#balance: number = 0
13+
14+
constructor(public accountNumber: string) {}
1615

1716
deposit(amount: number): void {
18-
this.balance += amount
17+
this.#balance += amount
1918
}
2019

2120
getBalance(): number {
22-
return this.balance
21+
return this.#balance
2322
}
2423
}
2524

@@ -39,6 +38,7 @@ console.log(`${admin.name} is a ${admin.role}`)
3938
const account = new BankAccount('12345')
4039
account.deposit(100)
4140
console.log(`Balance: $${account.getBalance()}`)
41+
// account.#balance would be an error - truly private!
4242

4343
const config = new Config()
4444
const customConfig = new Config('example.com', 8080, true)

exercises/02.access-modifiers/01.problem.public-private/README.mdx

Lines changed: 0 additions & 25 deletions
This file was deleted.

exercises/02.access-modifiers/01.problem.public-private/index.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

exercises/02.access-modifiers/01.problem.public-private/package.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

exercises/02.access-modifiers/01.solution.public-private/README.mdx

Lines changed: 0 additions & 7 deletions
This file was deleted.

exercises/02.access-modifiers/01.solution.public-private/index.test.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

exercises/02.access-modifiers/01.solution.public-private/index.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)