TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #3315 2.37K
CHALLENGE

class BankAccount {
#balance;
#transactionHistory = [];

constructor(initialBalance) {
this.#balance = initialBalance;
}

deposit(amount) {
this.#balance += amount;
this.#transactionHistory.push(`+${amount}`);
return this;
}

withdraw(amount) {
this.#balance -= amount;
this.#transactionHistory.push(`-${amount}`);
return this;
}

get summary() {
return `Balance: ${this.#balance} | Transactions: ${this.#transactionHistory.join(", ")}`;
}

static hasPrivateBalance(obj) {
return #balance in obj;
}
}

const account = new BankAccount(100);
account.deposit(50).deposit(25).withdraw(30);

console.log(account.summary);
console.log(BankAccount.hasPrivateBalance(account));
console.log(BankAccount.hasPrivateBalance({}));
  • ❤ 7
  • 🔥 5
  • 🤔 1
More from @javascript
  1. Sep 20, 2026Post #3507
  2. Sep 20, 2026CHALLENGE 'use strict'; function getValue() { return this.value; } const obj = { value: 42…
  3. Sep 19, 2026Post #3505
  4. Sep 19, 2026CHALLENGE const a = 5 & 3; const b = 5 | 2; const c = ~5; const d = 1 << 31; const e = -1…
  5. Sep 18, 2026Post #3503
  6. Sep 18, 2026CHALLENGE let i = 0; const key = () => `k${i++}`; const obj = { [key()]: i, [key()]: i, [k…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →