0
0
Javascriptprogramming~10 mins

Prototype chain in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prototype chain
Create object A
Set prototype of A to B
Access property on A
Check if property in A
Yes No
Return
Check if property in B
Return
When accessing a property, JavaScript looks first on the object, then up its prototype chain until it finds the property or reaches null.
Execution Sample
Javascript
const parent = {a: 1};
const child = Object.create(parent);
child.b = 2;
console.log(child.a);
console.log(child.b);
Create an object 'child' with 'parent' as prototype, then access properties from both.
Execution Table
StepActionObject CheckedProperty SearchedFound?Result
1Create object 'parent'N/AN/AN/A{a:1} created
2Create object 'child' with prototype 'parent'N/AN/AN/Achild created with prototype parent
3Assign property 'b=2' to childchildbN/Achild.b = 2
4Access child.achildaNoLook in prototype
5Access parent.aparentaYesValue 1 found
6Access child.bchildbYesValue 2 found
7EndN/AN/AN/AAll properties accessed
💡 Property found or prototype chain ends at null
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6Final
parentundefined{a:1}{a:1}{a:1}{a:1}{a:1}{a:1}
childundefinedundefinedObject with prototype parentObject with b=2Object with b=2Object with b=2Object with b=2
Key Moments - 3 Insights
Why does child.a return 1 even though 'a' is not directly on child?
Because child’s prototype is parent, and JavaScript looks up the prototype chain to find 'a' in parent (see execution_table rows 4 and 5).
What happens if a property is not found on the object or any prototype?
JavaScript returns undefined after reaching the end of the prototype chain (null). This is shown in the exit_note.
Does assigning child.b = 2 affect the parent object?
No, child.b is its own property and does not change parent (see execution_table row 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the property 'a' found when accessing child.a?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Check the 'Found?' and 'Result' columns for child.a access in steps 4 and 5.
According to variable_tracker, what is the value of 'child' after step 3?
AUndefined
BObject with prototype parent
CObject with b=2
DObject with a=1
💡 Hint
Look at the 'After Step 3' column for 'child' in variable_tracker.
If we tried to access child.c which does not exist, what would happen according to the prototype chain?
AReturn value from parent.c
BReturn undefined after checking all prototypes
CThrow an error
DReturn null
💡 Hint
Refer to the exit_note about property not found in prototype chain.
Concept Snapshot
Prototype chain in JavaScript:
- Objects have a prototype link to another object.
- Property access checks own properties first.
- If not found, looks up the prototype chain.
- Stops when property found or prototype is null.
- Enables inheritance and shared properties.
Full Transcript
This visual trace shows how JavaScript uses the prototype chain to find properties. When accessing a property on an object, JavaScript first checks if the property exists directly on that object. If not found, it looks at the object's prototype, then the prototype's prototype, and so on until it finds the property or reaches the end of the chain (null). In the example, 'child' is created with 'parent' as its prototype. Accessing 'child.a' looks up to 'parent' where 'a' is found. Accessing 'child.b' finds 'b' directly on 'child'. This chain allows objects to share properties and methods efficiently.