How JavaScript Boxes Primitive Values: Prototypes, __proto__, and the Wrapper Class Illusion
Understanding JavaScript Objects, Prototypes, and Wrapper Classes
Preface
Previously we covered the scope chain, closures, and execution contexts. Objects, prototypes (prototype / __proto__), and wrapper classes are the core knowledge built on JavaScript's object mechanism. Many people confuse the explicit prototype with the implicit one, and don't grasp why V8 automatically performs a new when a primitive value calls a method. This article follows the logic of object creation to tie together data types, constructors, prototype lookup, and wrapper classes.
1. JavaScript Data Types
It is often said that everything in JS is an object, but that's a loose statement. When we define a primitive literal value, V8 does not immediately execute new; only at the moment we access a property or method does it automatically execute a wrapper constructor to generate a temporary object. Data types are divided into two categories: primitive data types and reference data types.
Primitive data types: number, string, boolean, null, undefined, bigint, symbol
Reference data types: object, array, function
Primitive values themselves are not objects; reference types are essentially objects and can freely have properties and methods attached.
Example:
let num = 10
let str = 'hello'
let obj = { name: '张三' }
let arr = [1,2,3]
function fn(){}
Interpretation:
num and str are primitive values; when defined, they only store the value, no new happens. Only when accessing a method like str.toUpperCase() does V8 automatically perform new. obj, arr, and fn are reference-type objects.
2. Constructors: Batch-Creating Object Instances
To mass-produce object instances with the same structure, the most common approach is to use constructors.
A constructor is a function that can be called with new. JS provides native constructors like Object and Array.
function Person(name){
this.name = name
}
const p1 = new Person('小明')
const p2 = new Person('小红')
Execution flow:
When calling a constructor with new, a brand-new instance object is generated; the instance and the constructor are linked through prototypes, allowing multiple instances to reuse methods on the prototype.
3. prototype (Explicit Prototype) and proto (Implicit Prototype)
prototype: a property natively present on functions; it is itself an object.
__proto__: a property natively present on objects; it is itself an object.
Core equality: p1.__proto__ === Person.prototype (instance's implicit prototype equals constructor's explicit prototype).
V8's rule for looking up object properties: it first checks properties explicitly owned by the object itself; if not found, it follows __proto__ to look in the prototype object.
Person.prototype.say = function(){
console.log('你好')
}
p1.say()
Analysis: p1 itself doesn't have a say method; it follows p1.__proto__ to find say on Person.prototype.
4. Wrapper Classes
Primitive types cannot have properties or methods added to them, yet we can directly call methods on primitive values. That is the role of wrapper classes.
When we write a primitive value using a literal, no new happens at definition time; only when we access a property or method does V8 by default execute new Xxx(), creating a temporary object instance. After the operation, V8 immediately performs an unboxing operation and destroys the temporary object.
let s = 'test'
s.age = 18
console.log(s.age) // undefined
Analysis: Assigning s.age triggers boxing—the property is added to the temporarily generated wrapper object, and as soon as the assignment ends, the temporary object is destroyed. When reading again, boxing happens again, generating a brand-new temporary object, which cannot find the property, so it returns undefined.
Key result: The assignment statement does not throw an error; it looks as if the property was added, but reading it later yields
undefinedbecause the temporary object no longer holds it.
5. Common Questions
Do all types have wrapper classes? No. null and undefined have no wrapper classes; calling methods on them directly throws an error.
What is the difference between prototype and __proto__? prototype belongs to functions and is used to share methods with instances; __proto__ belongs to objects and is used to look up prototype properties upward, forming the basis of the prototype chain.
Why does adding a property to a primitive value not throw an error but the property can't be read? Boxing only creates a temporary object when a property or method is accessed. The property is added to the temporary object; once the object is destroyed, the data is lost. Accessing again creates a new, independent temporary wrapper object.
How to understand "everything is an object" in JS? This phrase does not mean primitive values are themselves objects. Reference types (arrays, functions, plain objects) are naturally objects. Primitive values like string, number, boolean, bigint, and symbol are called "objects" because of the wrapper class mechanism: when a method is read, auto-boxing temporarily turns the primitive into a wrapper instance object, allowing access to methods on the prototype chain, and their wrapper instance prototype chains ultimately trace back to Object.prototype. null and undefined are exceptions—they have no wrapper objects and are not objects. This phrase is a convenient informal summary, not a rigorous definition.
Summary
JS data types are divided into primitive and reference types; when defining a primitive literal, V8 does not immediately new; it does so only when accessing a property or method via wrapper classes. Constructors combined with new batch-create instances.
prototype is the function's explicit prototype; __proto__ is the object's implicit prototype; the instance's implicit prototype equals the constructor's explicit prototype.
Object property lookup searches own properties first, then follows __proto__ upward; wrapper classes use auto-boxing and unboxing to let primitive values call methods. Adding a property to a primitive value does not throw, but the property is stored on a temporary object that is destroyed, so reading it later yields undefined.
"Everything is an object" is a loose statement: reference types are objects themselves; primitive values are not objects, but because of wrapper classes they can be temporarily wrapped into objects and gain the ability to access prototype methods. So in casual speech, primitives are also included in "everything is an object", except for null and undefined.