OOP Design & Patterns — Series 3

Preview — 3 of 10 questions

What is logged?

javascript
class Cache {
  static #inst;
  constructor() {
    if (Cache.#inst) return Cache.#inst;
    this.data = new Map();
    Cache.#inst = this;
  }
}
class SubCache extends Cache {}

const a = new Cache();
const b = new SubCache();

console.log(a === b, b instanceof SubCache, b instanceof Cache);
Afalse true true
Btrue true true
Ctrue false true
Dfalse false true

What is logged?

javascript
class MyArray extends Array {
  static get [Symbol.species]() {
    return Array;
  }
}

const m = new MyArray();
m.push(1, 2, 3);
const mapped = m.map((x) => x);

console.log(m instanceof MyArray, mapped instanceof MyArray, mapped instanceof Array);
Atrue false true
Btrue true true
Cfalse false true
Dtrue false false

What is logged?

javascript
function Legacy() {
  if (!new.target) return new Legacy();
  this.ok = true;
}

const a = Legacy();
const b = new Legacy();
console.log(a.ok, b.ok, a instanceof Legacy);

class Modern {}
try {
  Modern();
} catch (e) {
  console.log(e.constructor.name);
}
Aundefined true false, then TypeError
Btrue true true, then nothing
Ctrue true false, then ReferenceError
Dtrue true true, then TypeError

Sign up free to play

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