The workshop-pass demo made separate objects from one class. Now model a bank account in script.js, checking each balance change in the Console.
Classes provide syntax for creating independent objects with shared methods. Private fields, getters, and setters come later in this module.
class WorkshopSession {
constructor(topic, seats) {
this.topic = topic;
this.seats = seats;
}
}
console.log(new WorkshopSession("Testing", 12).topic);
BankAccount constructor. The starter logs Alice’s owner name.getBalance() to return the balance; the starter checks the initial balance of 100.deposit(amount) add positive amounts; the starter deposits 50 into an empty account.deposit(amount) ignore negative amounts; the starter tries deposit(-20) on a balance of 100.withdraw(30) reduce a balance of 100 to 70 and return true. The starter checks both values.withdraw(100) on 50.withdraw(-1) on 50.toString() to return BankAccount(owner: Alice, balance: $120) after Alice’s deposit and withdrawal. The starter logs its result.mission result: BankAccount(owner: Alice, balance: $120) on its own Console line.Class syntax: class Name { constructor(...) { this.prop = val; } }
Methods go directly in the class body — no commas between them.
Create instances with new: const obj = new MyClass(args);