Classes
Access Modifiers
Access modifiers show which parts of a class are meant for callers and which are internal.
private
`private` marks a class member that should only be used inside the class.
Access Modifiers
access.ts
Replay: real traced execution (multi-file project)
class Wallet {
private cents: number;
constructor(startingCents: number) {
this.cents = startingCents;
}
deposit(cents: number): void {
this.cents = this.cents + cents;
}
balance(): number {
return this.cents;
}
}
const depositCents: number = 250;
const wallet: Wallet = new Wallet(1000);
wallet.deposit(depositCents);
console.log(`balance=${wallet.balance()}`);
class Wallet {
private cents: number;
constructor(startingCents: number) {
this.cents = startingCents;
}
deposit(cents: number): void {
this.cents = this.cents + cents;
}
balance(): number {
return this.cents;
}
}
const depositCents: number = 100;
const wallet: Wallet = new Wallet(1000);
wallet.deposit(depositCents);
console.log(`balance=${wallet.balance()}`);
class Wallet {
private cents: number;
constructor(startingCents: number) {
this.cents = startingCents;
}
deposit(cents: number): void {
this.cents = this.cents + cents;
}
balance(): number {
return this.cents;
}
}
const depositCents: number = 500;
const wallet: Wallet = new Wallet(1000);
wallet.deposit(depositCents);
console.log(`balance=${wallet.balance()}`);
depositCents ← 250, this.cents ← 1000, wallet ← [object Object]
4 constructor(startingCents: number) {5 this.cents = startingCents1000;6 }78 deposit(cents: number): void {9 this.cents = this.cents + cents;10 }1112 balance(): number {13 return this.cents;14 }15}1617const depositCents→ 250: number = 250; //@depositCents=100, 50018const wallet→ [object Object]: Wallet = new Wallet(1000);19wallet.deposit(depositCents250);values this step1000this.centsthis.cents ← 1250
5 this.cents = startingCents;6}78deposit(cents250: number): void {9 this.cents = this.cents→ 1250 + cents250;10}const wallet: Wallet = new Wallet(1000);
17const depositCents: number = 250; //@depositCents=100, 50018const wallet: Wallet = new Wallet(1000);19wallet.deposit(depositCents250);2021console.log(`balance=${wallet[object Object].balance()}`);balance(): number
9 this.cents = this.cents + cents;10}1112balance(): number {13 return this.cents1250;14}console.log(`balance=${wallet.balance()}`);
18const wallet: Wallet = new Wallet(1000);19wallet.deposit(depositCents);2021console.log(`balance=${wallet[object Object].balance()}`);outputbalance=1250
depositCents ← 100, this.cents ← 1000, wallet ← [object Object]
4 constructor(startingCents: number) {5 this.cents = startingCents1000;6 }78 deposit(cents: number): void {9 this.cents = this.cents + cents;10 }1112 balance(): number {13 return this.cents;14 }15}1617const depositCents→ 100: number = 100;18const wallet→ [object Object]: Wallet = new Wallet(1000);19wallet.deposit(depositCents100);values this step1000this.centsthis.cents ← 1100
5 this.cents = startingCents;6}78deposit(cents100: number): void {9 this.cents = this.cents→ 1100 + cents100;10}const wallet: Wallet = new Wallet(1000);
17const depositCents: number = 100;18const wallet: Wallet = new Wallet(1000);19wallet.deposit(depositCents100);2021console.log(`balance=${wallet[object Object].balance()}`);balance(): number
9 this.cents = this.cents + cents;10}1112balance(): number {13 return this.cents1100;14}console.log(`balance=${wallet.balance()}`);
18const wallet: Wallet = new Wallet(1000);19wallet.deposit(depositCents);2021console.log(`balance=${wallet[object Object].balance()}`);outputbalance=1100
depositCents ← 500, this.cents ← 1000, wallet ← [object Object]
4 constructor(startingCents: number) {5 this.cents = startingCents1000;6 }78 deposit(cents: number): void {9 this.cents = this.cents + cents;10 }1112 balance(): number {13 return this.cents;14 }15}1617const depositCents→ 500: number = 500;18const wallet→ [object Object]: Wallet = new Wallet(1000);19wallet.deposit(depositCents500);values this step1000this.centsthis.cents ← 1500
5 this.cents = startingCents;6}78deposit(cents500: number): void {9 this.cents = this.cents→ 1500 + cents500;10}const wallet: Wallet = new Wallet(1000);
17const depositCents: number = 500;18const wallet: Wallet = new Wallet(1000);19wallet.deposit(depositCents500);2021console.log(`balance=${wallet[object Object].balance()}`);balance(): number
9 this.cents = this.cents + cents;10}1112balance(): number {13 return this.cents1500;14}console.log(`balance=${wallet.balance()}`);
18const wallet: Wallet = new Wallet(1000);19wallet.deposit(depositCents);2021console.log(`balance=${wallet[object Object].balance()}`);outputbalance=1500
Follow the Deposit
- The wallet starts with
1000cents. depositCentsstarts at250.depositadds250to the stored cents.balance()returns the new cents value.- The program prints
balance=1250. | depositCents | starting cents | balance | | --- | --- | --- | | 100 | 1000 | 1100 | | 250 | 1000 | 1250 | | 500 | 1000 | 1500 |
Exercise: access.ts
Reproduce balance=1250, then use depositCents 100 and 500 to predict each balance.