Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
class BankAccount { #balance; #owner; #transactions = []; constructor(owner, initialBalance) { this.#owner = owner; this.#balance = initialBalance; } #recordTransaction(type, amount) { this.#transactions.push({ type, amount, date: new Date() }); } deposit(amount) { if (amount <= 0) throw new Error('Invalid amount'); this.#balance += amount; this.#recordTransaction('deposit', amount); } get balance() { return this.#balance; } get owner() { return this.#owner; } } const acc = new BankAccount('Alice', 1000); acc.deposit(500); console.log(acc.balance); // 1500 // console.log(acc.#balance); // SyntaxError
Result
Open