AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

05 Prototypes and classes / Checkpoint

Checkpoint — Prototypes and classes

24 min · +25 XP · 3 exercises · 12 questions

Exercises0/3
Quiz · 70% to clear12 q

This checkpoint covers the prototype chain and lookup, Object.create, shadowing, instanceof; constructor functions and the four steps of new; class syntax with fields, private members, accessors and statics; extends, super, construction order and extending built-ins; property descriptors and the conversion protocols.

How it works. Twelve questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • What a write to an inherited property does, and how in, Object.hasOwn and Object.keys differ.
  • The four steps of new, and what happens when a constructor returns an object.
  • Where fields, methods and statics live; what #private guarantees; why methods passed as callbacks lose this.
  • Why super() must come first, when subclass fields initialise, and what super.method() looks up.
  • The three descriptor flags and their defaults; which of toString/valueOf/Symbol.toPrimitive runs for ${x} and for x + 1.

The programs are an inventory built from a small class hierarchy with private state and JSON output, an event emitter with on/once/off/emit semantics, and a linked list class with a size accessor, static from, and toString.

Common questions

What happens when you assign to an inherited property in JavaScript?

The write creates or updates an own property on the object and never changes the prototype, so the new value shadows the inherited one; deleting the own property makes the inherited value visible again. The exception is a setter on the prototype, which runs instead.

What happens when a JavaScript constructor returns an object?

That object becomes the result of new, and the freshly created instance is discarded. A returned primitive is ignored, and new returns the new instance as usual.

Which method runs for ${x} and which for x + 1?

If Symbol.toPrimitive is defined it runs for both, with the hint "string" for the template literal and "default" for +. Without it, ${x} calls toString first and x + 1 calls valueOf first.

JavaScript Classes Quiz: Prototypes, new and super Practice

Test JavaScript prototypes and classes with 12 questions and three programs: the prototype chain, new, private fields, extends, super and descriptors.

  • Course: JavaScript study plan
  • Module: Prototypes and classes
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 24 min
  • Runtime: Node 16

Checkpoint — Prototypes and classes is the checkpoint that closes the Prototypes and classes module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the prototype chain and lookup, Object.create, shadowing, instanceof; constructor functions and the four steps of new; class syntax with fields, private members, accessors and statics; extends, super, construction order and extending built-ins; property descriptors and the conversion protocols.

How it works. Twelve questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • What a write to an inherited property does, and how in, Object.hasOwn and Object.keys differ.
  • The four steps of new, and what happens when a constructor returns an object.
  • Where fields, methods and statics live; what #private guarantees; why methods passed as callbacks lose this.
  • Why super() must come first, when subclass fields initialise, and what super.method() looks up.
  • The three descriptor flags and their defaults; which of toString/valueOf/Symbol.toPrimitive runs for ${x} and for x + 1.

The programs are an inventory built from a small class hierarchy with private state and JSON output, an event emitter with on/once/off/emit semantics, and a linked list class with a size accessor, static from, and toString.

Common questions

What happens when you assign to an inherited property in JavaScript?

The write creates or updates an own property on the object and never changes the prototype, so the new value shadows the inherited one; deleting the own property makes the inherited value visible again. The exception is a setter on the prototype, which runs instead.

What happens when a JavaScript constructor returns an object?

That object becomes the result of new, and the freshly created instance is discarded. A returned primitive is ignored, and new returns the new instance as usual.

Which method runs for ${x} and which for x + 1?

If Symbol.toPrimitive is defined it runs for both, with the hint "string" for the template literal and "default" for +. Without it, ${x} calls toString first and x + 1 calls valueOf first.

Exercises

An inventory hierarchy

Write Item(name, price, qty) with get value(), isExpired(day) (always false), describe(day) → <name> x<qty> @ <price 2dp> = <value 2dp> plus [expired] when expired, and toJSON() → { name, price, qty }; PerishableItem extends Item adds expiresDay, isExpired(day) is day > expiresDay, and its toJSON spreads super.toJSON() plus expiresDay; Inventory keeps items in a private Map by name with add, remove(name) (returns the Map's boolean), report(day) (items sorted by name, then total=<sum of non-expired values 2dp> items=<n> expired=<n>) and toJSON() returning the items array. Commands: item n p q, perishable n p q d, remove n (prints removed <n>=<bool>), report d, json (prints JSON.stringify(inv)).

Example:

item pen 1.50 10
perishable milk 2.25 4 3
perishable bread 3.00 2 5
report 4
remove pen
remove ghost
json

→

bread x2 @ 3.00 = 6.00
milk x4 @ 2.25 = 9.00 [expired]
pen x10 @ 1.50 = 15.00
total=21.00 items=3 expired=1
removed pen=true
removed ghost=false
[{"name":"milk","price":2.25,"qty":4,"expiresDay":3},{"name":"bread","price":3,"qty":2,"expiresDay":5}]

An event emitter

Implement Emitter with a private Map from event name to listener records { id, fn, once }: on(event, id, fn) (returns an unsubscribe function), once(event, id, fn), off(event, id) (returns whether something was removed), emit(event, payload) (calls every listener registered at the time of the call, in registration order, drops once listeners after their first call, and returns how many were called) and count(event). Script: on <event> <id> / once <event> <id> register a listener that prints <id>:<event>:<payload>; off <event> <id> prints off <event> <id> -> <bool>; emit <event> <payload> prints emit <event> -> <n> after the listeners ran; count <event> prints count <event> = <n>.

Example:

on save a
on save b
once save c
emit save first
emit save second
off save a
count save

→

a:save:first
b:save:first
c:save:first
emit save -> 3
a:save:second
b:save:second
emit save -> 2
off save a -> true
count save = 1

A linked list class

Implement LinkedList over Node { value, next } with private #head/#size: get size, static from(iterable), push, unshift, insertAt(i, v) and removeAt(i) (both throw RangeError("index out of range") for bad indices; removeAt returns the removed value), indexOf(v) (−1 when absent), reverse() in place, a generator [Symbol.iterator] yielding values, and toString() → [v1 -> v2 -> …] size=<n> ([] size=0 when empty). Commands: push v, unshift v, insert i v, remove i (prints removed=<v>), find v (prints index=<i>), reverse, show (prints String(list)), sum (prints sum=<total via the iterator>); print error: <message> for a thrown error.

Example: push 1, push 2, push 3, unshift 0, show, insert 2 9, show, remove 0, find 9, reverse, show, sum, remove 10 →

[0 -> 1 -> 2 -> 3] size=4
[0 -> 1 -> 9 -> 2 -> 3] size=5
removed=0
index=1
[3 -> 2 -> 9 -> 1] size=4
sum=15
error: index out of range

In this module: Prototypes and classes

← Property descriptors and the object protocols — toString, valueOf, toJSON, Symbol.toPrimitive · Map and Set — real keyed collections →