- JavaScript
- Execution Contexts, Closures, and Object Prototypes
- Prototypes & Inheritance
Prototype in JavaScript: Inheritance & Property Lookup
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}45Device.prototype.category = 'electronics';6Device.prototype.battery = 100;78const phone = new Device('Acme');910phone.battery = 85;1112const 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;1920console.log(res1, res2, res3, res4, res5);
Predict Console Output
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.
Prototype in JavaScript Explained
The Prototype Chain Lookup Algorithm in V8
When accessing a property obj.prop:
- The V8 engine checks the own properties (the object's internal shape / hidden class and property storage).
- If found, that value is returned immediately.
- If not found, V8 checks
obj.[[Prototype]](accessible viaObject.getPrototypeOf(obj)or__proto__). - If found on the prototype, that value is returned.
- If not found, the search continues recursively up the chain until it reaches
Object.prototype.[[Prototype]], which isnull. If still not found,undefinedis returned.
Property Shadowing vs Prototype Mutation
- Assignment creates own properties: Writing
phone.battery = 85does NOT mutateDevice.prototype. It creates a new own property onphonewith the value85. This is known as property shadowing. - Delete unmasks the prototype: Running
delete phone.batteryremoves the own property. Subsequent reads fall through toDevice.prototype.battery(100). - Delete never mutates prototypes: Running
delete phone.categoryattempts to delete an own propertycategoryfromphone. Sincephonedoes not owncategory, the operation is a harmless no-op and returnstrue, leavingDevice.prototype.categorycompletely untouched. - Inspection differences:
phone.hasOwnProperty('battery')returnsfalse(only own properties are considered), whereas'battery' in phonereturnstrue(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.
What is the difference between __proto__ and prototype?
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.
How does property shadowing work during assignment?
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.
What does Object.create(proto) do under the hood?
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.
How does the instanceof operator verify inheritance?
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.
Why does delete obj.prop not delete a property inherited from a prototype?
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.
Why is mutating __proto__ at runtime considered a severe performance anti-pattern?
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
