Prototype in JavaScript: Inheritance & Property Lookup

Medium100% Free~20 mins#prototypes#prototype-chain#inheritance#shadowing#hasOwnProperty#delete#v8
Interview Answer (30-Second Summary)

Every object has a prototype in JavaScript: inheritance works through prototype delegation, where each object holds an internal [[Prototype]] link to another object. Property access traverses this prototype chain upward to Object.prototype, while property assignment creates an own property on the receiver object, shadowing prototype properties.

Key Learning Objectives
✓

Understand the ECMAScript [[Prototype]] link and prototype chain traversal.

✓

Master property shadowing and how writing to an instance creates own properties.

✓

Learn why the delete operator only removes own properties and never prototype properties.

✓

Distinguish between Object.prototype.hasOwnProperty() and the 'in' operator.

The Interview Problem

What is logged to the console when the following code executes, and how does property deletion interact with shadowed prototype properties?

1function Device(brand) {
2 this.brand = brand;
3}
4
5Device.prototype.category = 'electronics';
6Device.prototype.battery = 100;
7
8const phone = new Device('Acme');
9
10phone.battery = 85;
11
12const res1 = phone.battery;
13delete phone.battery;
14const res2 = phone.battery;
15delete phone.category;
16const res3 = phone.category;
17const res4 = phone.hasOwnProperty('battery');
18const res5 = 'battery' in phone;
19
20console.log(res1, res2, res3, res4, res5);
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

85 100 electronics false true

85 undefined undefined false false

85 100 undefined true true

85 85 electronics false true

V8 Engine Execution Trace
Step 1 of 10 (Line 1)

Device constructor function is declared. V8 engine initializes Device.prototype as an empty object with a constructor property pointing back to Device.

Call Stack (Top = Active)
Global Execution Context
Lexical Scope / Bindings
Device:FunctionRef#Device
Console Stream
> [empty]

Prototype in JavaScript Explained

The Prototype Chain Lookup Algorithm in V8

When accessing a property obj.prop:

  1. The V8 engine checks the own properties (the object's internal shape / hidden class and property storage).
  2. If found, that value is returned immediately.
  3. If not found, V8 checks obj.[[Prototype]] (accessible via Object.getPrototypeOf(obj) or __proto__).
  4. If found on the prototype, that value is returned.
  5. If not found, the search continues recursively up the chain until it reaches Object.prototype.[[Prototype]], which is null. If still not found, undefined is returned.

Property Shadowing vs Prototype Mutation

  • Assignment creates own properties: Writing phone.battery = 85 does NOT mutate Device.prototype. It creates a new own property on phone with the value 85. This is known as property shadowing.
  • Delete unmasks the prototype: Running delete phone.battery removes the own property. Subsequent reads fall through to Device.prototype.battery (100).
  • Delete never mutates prototypes: Running delete phone.category attempts to delete an own property category from phone. Since phone does not own category, the operation is a harmless no-op and returns true, leaving Device.prototype.category completely untouched.
  • Inspection differences: phone.hasOwnProperty('battery') returns false (only own properties are considered), whereas 'battery' in phone returns true (the entire prototype chain is inspected).
Common Traps & Mistakes

Thinking `delete instance.prop` will delete a property on the prototype. To delete a prototype property, you must explicitly call `delete Constructor.prototype.prop`.

Confusing `__proto__` with `.prototype`. `.prototype` is an own property on constructor functions used as the blueprint for instances; `[[Prototype]]` (or `__proto__`) is the actual link on instances.

Mutating shared objects on prototypes: if `Device.prototype.settings = {}`, all instances share the exact same object reference in heap memory, causing shared mutation bugs.

FAANG Follow-Up Probes
Probe #1

How does `Object.create(proto)` differ from `new Constructor()` in setting up prototype delegation?

Probe #2

What is Prototype Pollution, how does it occur with recursive object merges, and how do you protect against it using `Object.create(null)` or `Map`?

Probe #3

How does modern ES6 `class extends` desugar into prototype inheritance, `super()`, and `Object.setPrototypeOf()` under the hood?

Prototype in JavaScript Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for Prototypes & Inheritance.

Q1.
What is the prototype chain in JavaScript?

The prototype chain is a series of linked objects connected via their internal [[Prototype]] references. When reading a property on an object, the engine checks the object itself; if missing, it checks its prototype, and so on until finding the property or reaching null.

prototype is a property found only on constructor functions that defines the prototype object assigned to instances created via new. __proto__ is an accessor property on Object.prototype that exposes the internal [[Prototype]] of an object instance.

Setting a property on an object (obj.x = 10) creates an own property on obj even if property x exists further up the prototype chain. The own property now shadows (hides) the prototype property during subsequent reads.

Object.create(proto) creates a brand new empty object whose internal [[Prototype]] link points directly to the provided proto argument, establishing clean prototype inheritance without invoking constructor functions.

instanceof checks if the prototype property of a constructor function appears anywhere along the prototype chain of the target object instance by traversing the [[Prototype]] links.

The delete operator only deletes own properties on the target object itself. It never traverses or mutates prototype objects in the chain. If a shadowed property is deleted, subsequent reads resolve to the prototype value again.

Mutating an object's prototype alters its hidden class (Shape/Map) in V8, invalidating inline caches and deoptimizing all JIT-compiled machine code that accesses properties on objects sharing that prototype chain.

More JavaScript interview questions for freshers