跪拜 Guibai
← All articles
JavaScript

Why 'hello'.a Is undefined: JS Objects, Prototypes, and Boxing Explained

By 爱编程的17 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Misreading reference vs. value and prototype sharing causes subtle bugs in state management and inheritance; mastering these mechanics lets a developer reason about any object code without guessing. The article turns common interview questions into predictable rules, making it a practical grounding for writing reliable JavaScript.

Summary

JavaScript's object semantics come down to three rules: primitives are values, objects are references, and property lookup walks the prototype chain. The wrapper-class trick explains why reading a primitive's property works but writing one silently vanishes. The article walks through real code: 'hello'.length is 5, but 'hello'.a = 2 leaves no trace; two identical-looking object literals fail === equality; and new links an instance to its constructor's prototype in four steps.

Property resolution moves from the object itself up through __proto__ to Object.prototype, and stops at null. Methods belong on the prototype so all instances share one copy; data belongs on the instance so mutations stay isolated. The prototype chain also exposes a classic gotcha: putting an array on the prototype means every instance sees the same array, so pushing through one instance pollutes them all.

Primitives are not objects, but reading a property triggers a temporary boxed String or Number object that is discarded after the access. That's why assigning a property to a primitive is silently lost in sloppy mode and throws in strict mode. Modern tooling includes structuredClone for deep copies, while JSON serialization drops functions, undefined, and symbols, and mangles Date and RegExp.

Takeaways
— typeof null returns 'object' due to a legacy bug; use x === null to test null.
— Objects are compared by reference, not content; two identical-looking literals are unequal with ===.
— Assigning an object variable copies the reference, so mutating via one variable affects the other.
— new runs four steps: create object, link __proto__ to constructor's prototype, execute constructor with this, return object unless a replacement is returned.
— Instance's __proto__ equals constructor's prototype; property lookup goes up the chain to Object.prototype and null.
— Methods should live on the prototype for sharing; data lives on the instance to avoid cross-instance contamination.
— Primitives aren't objects; reading a property like length temporarily boxes the primitive in a wrapper object that is discarded after the read, so assignments to primitives are lost.
— In strict mode, assigning a property to a primitive throws a TypeError.
— structuredClone provides deep copying; JSON.parse(JSON.stringify()) drops functions, undefined, symbols, and mangles Date and RegExp.
— class syntax is sugar over prototypes; super.speak() delegates to Animal.prototype.
Conclusions

The wrapper-class mechanism is a hidden cost of JavaScript's convenience: every property read on a primitive allocates and discards a temporary object, and strict mode turns the silent failure into a loud exception.

Shared reference types on prototypes are a footgun: the article's example shows how one instance's push pollutes all siblings, which is why the guidance to keep data on instances is a concrete rule, not style advice.

JavaScript's compatibility quirks (typeof null, referential equality) are deliberate trade-offs that force developers to learn mental models instead of relying on intuition, and the article's systematic walkthrough turns these quirks into predictable rules.

Concepts & terms
Wrapper class
A temporary object created by the JavaScript engine when a property is accessed on a primitive. It allows reading methods like length, but is discarded after the read, so writes to it are lost.
Prototype chain
The chain of __proto__ links from an object up to Object.prototype, used for property lookup. Shared methods live on prototypes; data on instances.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗