Advanced OOP — Series 3

Preview — 3 of 10 questions

What is logged?

javascript
class Temp {
  #celsius = 0;
  #format() {
    return `${this.#celsius}C`;
  }
  static isTemp(o) {
    return #celsius in o;
  }
  toString() {
    return this.#format();
  }
}

console.log(String(new Temp()), Temp.isTemp(new Temp()), Temp.isTemp({}));
A0C true false
B0C true true
C[object Object] true false
DTypeError: Cannot read private member #celsius from an object whose class did not declare it

What is logged?

javascript
class Duration {
  constructor(ms) {
    this.ms = ms;
  }
  [Symbol.toPrimitive](hint) {
    if (hint === 'number') return this.ms;
    if (hint === 'string') return `${this.ms}ms`;
    return 'default';
  }
}

const d = new Duration(500);
console.log(+d, `${d}`, d + '');
A500 500ms 500ms
B500 500 default
C500 500ms default
D500ms 500ms default

What is logged?

javascript
const order = [];

class Config {
  static #raw = 'a,b';
  static list;
  static {
    order.push('static block');
    Config.list = Config.#raw.split(',');
  }
  constructor() {
    order.push('constructor');
  }
}

order.push('after class');
new Config();

console.log(order.join(' | '), Config.list.length);
Aafter class | static block | constructor and 2
Bstatic block | after class | constructor and 2
Cafter class | constructor | static block and 2
Dstatic block | after class | constructor and 0

Sign up free to play

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