Study notes · 10% of the exam

Objects, Prototypes and Classes

Objects are bags of properties linked into prototype chains. Know how lookup, assignment and definition differ, what classes really create, and the order in which constructors and fields run.

Key points

  1. 1

    Reads walk the prototype chain; plain assignment normally creates an own property that shadows the inherited one. An inherited setter or read-only property changes that: the setter runs instead, or the write fails (throwing in strict mode).

  2. 2

    Every property has attributes: data properties have value/writable, accessors have get/set, and both have enumerable/configurable. Assignment creates all-true attributes; Object.defineProperty defaults omitted ones to false.

  3. 3

    Own keys are ordered: array-index keys ascending, then other strings in insertion order, then symbols. Object.keys = own enumerable strings, Reflect.ownKeys = every own key, for...in = enumerable strings including inherited ones.

  4. 4

    new F() links a fresh object to F.prototype, runs F with it as this, and returns it unless F returns an object. Classes add rules: they must be called with new, their bodies are strict, and statics are inherited through Child → Base.

  5. 5

    Field order: in a base class, fields are set before the constructor body; in a derived class, this is unusable until super() returns, and then the derived fields are initialised. Fields use define semantics, so they skip inherited setters.

  6. 6

    Private #names are not properties: freeze, spread and Object.keys don't see them, #x in obj is a brand check, and reading a missing private field throws a TypeError.

  7. 7

    Object.assign uses get/set (getters run, target setters run). Object spread defines data properties on a new plain object. Both are shallow and drop the prototype; copy descriptors to keep accessors, and use structuredClone for deep copies of plain data.

Common traps

  • Calling an overridden method from a base constructor runs the subclass method before the subclass's fields exist.

  • obj.__proto__ = x only changes the prototype when obj inherits the __proto__ accessor. Null-prototype objects and computed or JSON keys create an ordinary property instead, which is the root of prototype-pollution bugs.

  • this.constructor.count++ on an inherited static creates a new own static on the subclass instead of updating the base class's counter.

Test yourself on Objects, Prototypes and Classes

Ten questions, with the answer and explanation after each one.