Advanced OOP — Series 2

Preview — 3 of 10 questions

What does this log?

javascript
class Counter {
  #count = 0;
  increment() { return ++this.#count; }
}
const c = new Counter();
console.log(c.count);
Aundefined
B0
CThrows a SyntaxError
DThrows a TypeError

What hint does the binary + operator pass to [Symbol.toPrimitive]?

javascript
class Money {
  constructor(amount) { this.amount = amount; }
  [Symbol.toPrimitive](hint) {
    if (hint === 'number') return this.amount;
    if (hint === 'string') return `$${this.amount}`;
    return `default:${this.amount}`;
  }
}
const m = new Money(50);
console.log(m + 10);
A'number'
B'default'
C'string'
Dundefined

When does the static { ... } block run?

javascript
class Config {
  static apiUrl;
  static {
    Config.apiUrl = 'https://api.example.com';
  }
}
AEvery time new Config() is called
BLazily, on first access to any static property
COnce, when the class itself is defined/evaluated — not per instance
DOnly if the class is exported from a module

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.