Chapter 4 · Real programs

Classes

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A class writes the links for you

By the end of this lesson you can:

  • write a class and make objects from it with new,
  • keep a field private,
  • extend a class and override its methods.

Under it all are lesson 16's links: a class builds them for you.

class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
}
const bo = new Account("Bo");
bo.deposit(5);
console.log(bo);
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
}
const a = new Account("Ada");
const b = new Account("Bo");
a.deposit(5);
b.deposit(2);
a.deposit(1);
console.log(a.balance);
console.log(b.balance);
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
  // withdraw(amount): refuse more than the balance
}
const a = new Account("Ada");
a.deposit(5);
a.withdraw(10);
a.withdraw(2);
console.log(`Balance: ${a.balance}`);
class Account {
  #balance = 0;
  constructor(owner) {
    this.owner = owner;
  }
  deposit(amount) {
    this.#balance += amount;
  }
  getBalance() {
    return this.#balance;
  }
}
const a = new Account("Ada");
a.deposit(5);
console.log(a);
console.log(a.#balance);
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
  toString() {
    return `${this.owner}: ${this.balance}`;
  }
}
const a = new Account("Ada");
a.deposit(5);
console.log(`${a}`);
console.log(a);
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
}
const a = new Account("Ada");
a.deposit(5);
const deposit = a.deposit;
deposit(5);
class Basket {
  constructor() {
    this.total = 0;
  }
  addAll(prices) {
    prices.forEach((p) => {
      this.total += p;
    });
  }
}
const b = new Basket();
b.addAll([2, 3, 5]);
console.log(b.total);
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
}
class Savings extends Account {
  constructor(owner, rate) {
    super(owner);
    this.rate = rate;
  }
  deposit(amount) {
    super.deposit(amount + 1);
  }
}
const s = new Savings("Bo", 2);
s.deposit(10);
console.log(s.balance);
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
  withdraw(amount) {
    if (amount > this.balance) {
      console.log("Refused");
    } else {
      this.balance -= amount;
    }
  }
}
// 1. class GiftCard extends Account
// 2. its toString(): "Ada's card: 20.00"
// 3. a card of 20: withdraw, log, withdraw, log
class Account {
  constructor(owner) {
    this.owner = owner;
    this.balance = 0;
  }
  deposit(amount) {
    this.balance += amount;
  }
  withdraw(amount) {
    if (amount > this.balance) {
      console.log("Refused");
    } else {
      this.balance -= amount;
    }
  }
}
const a = new Account("Ada");
a.deposit(5).withdraw(2).deposit(10);
console.log(a.balance);
Terminal
Step 1 of 10A new account

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)