跪拜 Guibai
← All articles
Frontend · JavaScript

JS Objects Explained: Literals, Constructors, Prototype Chains, and the Wrapper Classes Behind str.a

By 曹一二 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Misunderstanding the object model produces subtle bugs like silently lost properties and confusing equality checks. Knowing how V8 handles primitives, the prototype chain, and temporary wrappers gives developers a debugging edge when writing or refactoring JavaScript code.

Summary

JavaScript's object model trips up developers with behaviors like assigning a property to a string and reading back undefined. The explanation lies in how the language separates primitive values from reference types. Primitive values are compared by value, while objects are compared by address. This foundational distinction drives the way equality works, including why two identical-looking Symbols are never equal.

The article walks through six code experiments. Object literals and the three CRUD operations on key-value pairs are straightforward, but the real depth comes from constructors and the prototype chain. The `new` keyword does four hidden steps: create a blank object, wire its `__proto__` to the constructor's `prototype`, bind `this`, and return the object. All instances share methods placed on the prototype, and property lookup follows the chain from the object itself up to `Object.prototype` and then `null`.

The final piece is the wrapper class mechanism. Accessing a property on a primitive like `'hello'.length` triggers V8 to create a temporary wrapper object, attach the accessed property, and discard it immediately. That's why `str.a = 2` doesn't throw but also doesn't stick; the primitive itself never changes. In strict mode, the same action throws a TypeError.

Takeaways
— JS has 7 primitive types compared by value and reference types (Object, Array, Function) compared by address.
— An object is a collection of key-value pairs; adding or modifying uses `obj.key = value`, deleting uses `delete obj.key`, and a variable as a key requires square brackets.
— `{}` and `new Object()` are equivalent; `new` on a constructor performs four steps: create empty object, link `__proto__` to the constructor's prototype, bind `this`, and return the object.
— Every function has a `prototype` property; every object has a `__proto__` property. The core equation is instance.__proto__ === constructor.prototype.
— Property lookup goes from the object itself up the prototype chain until null; `hasOwnProperty` distinguishes own vs inherited properties.
— Arrays are objects; methods like `push` are found on `Array.prototype` via the prototype chain.
— Primitives cannot hold custom properties. Accessing a property on a primitive creates a temporary wrapper object that is destroyed immediately after the operation, so assignments silently vanish.
— In strict mode, adding a property to a primitive throws a TypeError instead of failing silently.
— The modern replacement for `__proto__` is `Object.getPrototypeOf`.
Conclusions

The silent failure of setting a property on a primitive is a deliberate design choice: primitives stay lightweight, and the wrapper is ephemeral. This behavior neatly illustrates the boundary between JS's value types and object types.

Understanding the four steps of `new` is more useful than memorizing constructor syntax; it explains why methods belong on the prototype and why `this` in a constructor represents the new instance.

The prototype chain is essentially a shared memory mechanism; putting methods on the prototype saves memory but also means the same method is shared by all instances, a key performance consideration in larger applications.

Concepts & terms
prototype chain
A chain of objects linked via __proto__. When accessing a property, JavaScript first looks at the object itself, then follows the chain of prototype objects until it finds the property or reaches null.
wrapper class
A temporary object created by JavaScript when you access a property or method on a primitive (like string, number, boolean). The operation is performed on this wrapper, and the wrapper is destroyed immediately after.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗