跪拜 Guibai
← All articles
JavaScript

How JavaScript Boxes Primitive Values: Prototypes, __proto__, and the Wrapper Class Illusion

By 凌晨三点说晚安 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Auto-boxing and the prototype chain are the root of two easy-to-miss bugs: silently lost property assignments on primitives and unexpected method lookup behavior. Once you know exactly when V8 creates and destroys wrapper objects, you can predict these outcomes instead of being surprised by them.

Summary

JavaScript's object system separates primitive values (number, string, boolean, bigint, symbol) from reference types (object, array, function). Primitives are not objects at definition time; V8 defers object creation until a property or method is accessed, at which point it runs the corresponding wrapper constructor and produces a disposable temporary object. That mechanism, called auto-boxing, lets you call 'str'.toUpperCase(), but it also means any property you write to a primitive lands on a temporary object that is destroyed before you can read it back, so s.age = 18 followed by console.log(s.age) yields undefined.

Constructors, when invoked with new, produce instances whose __proto__ points to the constructor's prototype. Property lookup checks own properties first, then climbs __proto__ up the prototype chain, which is why methods defined on Person.prototype are shared by all Person instances. The article walks through the exact execution sequence, clarifies that only functions have prototype and only objects have __proto__, and notes that null and undefined have no wrapper classes, so calling methods on them throws. The common phrase 'everything is an object' holds only through the temporary boxing illusion; reference types are objects by nature, while primitives merely borrow object behavior for the duration of a single property access.

Takeaways
— Primitive types (number, string, boolean, bigint, symbol) are not objects; reference types (object, array, function) are.
— V8 does not call a constructor when a primitive literal is defined; boxing happens only on property or method access.
— Constructors used with new create instances; an instance's __proto__ equals the constructor's prototype.
— Property lookup searches own properties first, then follows __proto__ up the prototype chain.
— Wrapper classes create a temporary object for primitive method calls; that object is destroyed after the operation.
— Assigning a property to a primitive triggers boxing but the property is lost because the wrapper is temporary; reading it later returns undefined.
— null and undefined have no wrapper classes, so calling methods on them throws.
Conclusions

The 'everything is an object' motto is a convenience that only works because of auto-boxing; the illusion collapses the moment you try to persist a property on a primitive.

The temporary wrapper's lifecycle explains both why method calls work and why property writes fail: they go through the same object, but the object is gone before you can observe the write.

The distinction between prototype and __proto__ is a frequent source of confusion because one is a function property and the other is an object property, but the equality p1.__proto__ === Person.prototype ties them together.

Concepts & terms
Wrapper class (包装类)
A temporary object V8 creates when a primitive value's property or method is accessed, enabling primitive method calls. The object is destroyed immediately after the operation.
Explicit prototype (prototype)
A property on functions; it holds methods and properties shared by instances created through that function.
Implicit prototype (__proto__)
A property on all objects that points to the constructor's prototype, forming the chain for property lookup.
Auto-boxing
The automatic creation of a wrapper object around a primitive when a property or method is accessed, followed by immediate destruction after the operation.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗