0
0
Javascriptprogramming~10 mins

Why prototypes are needed in Javascript - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why prototypes are needed
Create Object
Access Property
Property Found?
NoLook in Prototype
Property Found?
Use Property
Done
Repeat Until Found or null
Return undefined if not found
When you try to use a property on an object, JavaScript looks for it on the object itself. If it is not there, it looks up the chain in the object's prototype, repeating until it finds the property or reaches the end.
Execution Sample
Javascript
const obj = {a: 1};
console.log(obj.a); // 1
console.log(obj.b); // undefined

obj.__proto__.b = 2;
console.log(obj.b); // 2
This code shows how JavaScript looks for properties on the object and then on its prototype if not found.
Execution Table
StepActionProperty AccessedFound OnValue Returned
1Access obj.aaobj1
2Access obj.bbobjundefined
3Add b=2 to obj.__proto__bobj.__proto__2
4Access obj.b againbobj.__proto__2
💡 Property b not found on obj initially, found on prototype after adding.
Variable Tracker
VariableStartAfter Step 3After Step 4
obj{a:1}{a:1}{a:1}
obj.__proto__{}{b:2}{b:2}
Key Moments - 2 Insights
Why does obj.b return undefined at first but 2 after adding b to obj.__proto__?
At step 2 in the execution table, obj.b is not found on obj, so it returns undefined. After step 3, b is added to obj.__proto__, so at step 4, obj.b finds b in the prototype and returns 2.
Why does JavaScript look in obj.__proto__ when a property is not found on obj?
JavaScript uses the prototype chain to find properties. If a property is missing on the object itself, it checks the prototype (obj.__proto__) as shown in the concept flow and execution table steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does obj.b have at step 2?
Aundefined
B2
Cnull
D1
💡 Hint
Check the 'Value Returned' column for step 2 in the execution_table.
At which step is the property b added to the prototype?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column describing adding b to obj.__proto__.
If we remove the line adding b to obj.__proto__, what would obj.b return at step 4?
Anull
B2
Cundefined
D1
💡 Hint
Refer to step 2 where obj.b is undefined before adding b to prototype.
Concept Snapshot
JavaScript objects have prototypes.
When a property is not found on the object, JS looks up the prototype chain.
This allows sharing properties and methods efficiently.
Prototypes help avoid duplication and enable inheritance.
Accessing a property triggers this lookup automatically.
Full Transcript
This visual execution shows why prototypes are needed in JavaScript. When you try to access a property on an object, JavaScript first looks on the object itself. If it does not find the property, it looks on the object's prototype, then the prototype's prototype, and so on until it finds the property or reaches the end of the chain. The example code shows accessing obj.a which is found on obj, and obj.b which is initially undefined. After adding b to obj's prototype, accessing obj.b returns 2. This demonstrates how prototypes allow objects to share properties and methods without duplicating them. The execution table traces each step of property lookup and modification. The variable tracker shows how obj and its prototype change. Key moments clarify why property lookup works this way. The quiz tests understanding of the prototype lookup process. Overall, prototypes are needed to enable efficient property sharing and inheritance in JavaScript.